Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Last active June 15, 2021 10:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save greathmaster/4f044cebdee6e9c978f936bc56d54aba to your computer and use it in GitHub Desktop.
Save greathmaster/4f044cebdee6e9c978f936bc56d54aba to your computer and use it in GitHub Desktop.
Checks birthday on checkout to confirm member is over 21 years.
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(
'dob',
'date',
array(
'label' => 'Birthday',
'size' => 40,
'profile' => true,
'required' => true,
'hint' => 'You must be over 21 to signup.',
)
);
//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
);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(
'dob',
'date',
array(
'label' => 'Birthday',
'size' => 40,
'profile' => true,
'required' => true,
'hint' => 'You must be over 21 to signup.',
)
);
//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' );
//require the fields
function my_pmpro_registration_checks()
{
global $pmpro_msg, $pmpro_msgt, $current_user;
$birthday = $_REQUEST['dob'];
$month =str_pad($birthday['m'], 2, '0', STR_PAD_LEFT);
$day = str_pad($birthday['d'], 2, '0', STR_PAD_LEFT);
$year = $birthday['y'];
$age = get_age($year.'-'.$month.'-'.$day);
if(!validateDate($year.'-'.$month.'-'.$day))
{
$pmpro_msg = "Please enter a valid birthday";
$pmpro_msgt = "pmpro_error";
return false;
}
if($age > 20 || $current_user->ID)
{
//all good
return true;
}
else
{
$pmpro_msg = "You must be over 21 years old to signup";
$pmpro_msgt = "pmpro_error";
return false;
}
}
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
//Taken from: http://gazelleincorporated.com/how-to-calculate-age-from-date-of-birth-in-php
//$dob = birthdate in the form "YYYY-MM-DD"
//Extract the month, day and year from the $dob
//Compare to current month, day, year to get AGE
function get_age($dob)
{
$dob=explode("-",$dob);
$curMonth = date("m");
$curDay = date("j");
$curYear = date("Y");
$age = $curYear - $dob[0];
if($curMonth<$dob[1] || ($curMonth==$dob[1] && $curDay<$dob[2]))
$age--;
return $age;
}
//Taken from: https://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
//that's it. see the PMPro Register Helper readme for more information and examples.
}
add_action( 'init', 'my_pmprorh_init' );
//require the fields
function my_pmpro_registration_checks()
{
global $pmpro_msg, $pmpro_msgt, $current_user;
$birthday = $_REQUEST['dob'];
$age = get_age($birthday['y'].'-'.$birthday['m'].'-'.$birthday['d']);
if($age > 20 || $current_user->ID)
{
//all good
return true;
}
else
{
$pmpro_msg = "You must be over 21 years old to signup";
$pmpro_msgt = "pmpro_error";
return false;
}
}
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
//Taken from: http://gazelleincorporated.com/how-to-calculate-age-from-date-of-birth-in-php
//$dob = birthdate in the form "YYYY-MM-DD"
//Extract the month, day and year from the $dob
//Compare to current month, day, year to get AGE
function get_age($dob)
{
$dob=explode("-",$dob);
$curMonth = date("m");
$curDay = date("j");
$curYear = date("Y");
$age = $curYear - $dob[0];
if($curMonth<$dob[1] || ($curMonth==$dob[1] && $curDay<$dob[2]))
$age--;
return $age;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment