-
-
Save MaryOJob/4fc1f3a81cfbb264f1161b70d2f99e35 to your computer and use it in GitHub Desktop.
Checks birthday on checkout to confirm member is over 21 years.
This file contains hidden or 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
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