Created
February 14, 2020 12:30
-
-
Save femiyb/50a7ef022e3715723423a83a8ac87d4c to your computer and use it in GitHub Desktop.
Set default date and check valid date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* PMPro Customization: Register Helper - Add date of birth date picker to checkout | |
*/ | |
//we have to put everything in a function called on init, so we are sure Register Helper is loaded | |
function my_pmprorh_init() | |
{ | |
//don't break if Register Helper is not loaded | |
if(!function_exists( 'pmprorh_add_registration_field' )) { | |
return false; | |
} | |
//define the fields | |
$fields = array(); | |
$fields[] = new PMProRH_Field( | |
'birthday', // input name, will also be used as meta key | |
'date', // type of field | |
array( | |
'label' => 'Date Of Birth', // custom field label | |
'class' => 'date', // date class | |
'profile' => true, // show in user profile | |
'required' => true, // make this field required | |
// 'addmember' => true, // Uncomment to display in Add Member | |
// 'memberslistcsv' => true, // Uncomment to Include in Member List CSV Export | |
) | |
); | |
//add the fields into a new checkout_boxes are of the checkout page | |
foreach($fields as $field) | |
pmprorh_add_registration_field( | |
'checkout_boxes', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
//that's it. See the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmprorh_init' ); | |
function set_default_register_helper_date() | |
{ | |
//Assumes RH field with key = "birthday" | |
?> <script> | |
jQuery('#birthday_m').val(1); | |
jQuery('#birthday_d').val('mm'); | |
jQuery('#birthday_y').val('yyyy'); | |
</script> | |
<?php | |
} | |
add_action("pmpro_checkout_after_form", "set_default_register_helper_date"); | |
function custom_pmpro_validate_user_age( $okay ) { | |
if( ! $okay ) { | |
return $okay; //handle other errors first. | |
} | |
global $pmpro_msg, $pmpro_msgt; | |
$date = $_POST[ 'birthday' ]; | |
$year = $date[ 'y' ]; | |
if( $year == 'yyyy' ){ | |
$pmpro_msg = "Please select a valid date."; | |
$pmpro_msgt = "pmpro_error"; | |
return false; | |
} | |
return $okay; | |
} | |
add_filter( 'pmpro_registration_checks', 'custom_pmpro_validate_user_age' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment