Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Created September 8, 2018 14:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbrocks/47267d441b36c98036d80388fa21a47f to your computer and use it in GitHub Desktop.
Save pbrocks/47267d441b36c98036d80388fa21a47f to your computer and use it in GitHub Desktop.
Add hidden PMPro registration field that shows a message when a checkbox is ticked.
jQuery(document).ready(function($) {
var label = $('label[for=hidden_check]');
$(label).hide();
$('#date_check').click(function() {
if($('#date_check').is(':checked')) {
$(label).css('color','salmon').show();
$('input[name=hidden_check]').val('1');
} else {
$(label).hide();
$('input[name=hidden_check]').val('0');
}
});
});
<?php // Do not include in your customizations plugin
/**
* Add hidden PMPro registration field that shows a message when a checkbox is ticked.
* Requires that you also enqueue the attached JS file from a folder in the plugin that you add this recipe.
*/
function register_helper_hidden_field() {
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// define the fields
$fields = array();
// HTML
$fields[] = new PMProRH_Field(
'hidden_check',
'hidden',
array(
'id' => 'hidden_check',
'label' => 'WooHoo, Confirmed!',
'profile' => true,
)
);
$fields[] = new PMProRH_Field(
'date_of_birth',
'date',
array(
'label' => 'Date Of Birth',
'size' => 40,
'profile' => true,
)
);
// CHECKBOX
$fields[] = new PMProRH_Field(
'date_check',
'checkbox',
array(
'text' => '<span id="change-result">Yes, I have confirmed my DOB</span>',
'id' => 'date_check',
'label' => 'Date confirmation',
'profile' => true,
'required' => true,
)
);
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'checkout_boxes',
$field
);
}
}
add_action( 'init', 'register_helper_hidden_field' );
/**
* enqueue_check_date_change Tell WordPress to load a javascript snippet named date-check.js that is saved in the js folder for the plugin where this file is saved.
*
* @return null
*/
function enqueue_check_date_change() {
wp_register_script(
'check-date-field', plugins_url( 'js/date-check.js', __FILE__ ), array( 'jquery' ), time(), true
);
wp_enqueue_script( 'check-date-field' );
}
add_action( 'admin_enqueue_scripts', 'enqueue_check_date_change' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment