Skip to content

Instantly share code, notes, and snippets.

@femiyb
Created February 14, 2020 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save femiyb/50a7ef022e3715723423a83a8ac87d4c to your computer and use it in GitHub Desktop.
Save femiyb/50a7ef022e3715723423a83a8ac87d4c to your computer and use it in GitHub Desktop.
Set default date and check valid date
<?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