Created
June 5, 2024 11:27
-
-
Save ipokkel/891f2cdf08ce67279c746be63233a73b to your computer and use it in GitHub Desktop.
Check that a user has filled in a valid birthday date and that the date of birth falls inside a required set date range for the level they are checking out.
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
<?php | |
/** | |
* Require a specific age range for some membership levels. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_required_age_range( $okay ) { | |
// Set the age range required per level. | |
$age_required_per_level = array( | |
// Level 3 requires a minimum age of 21 and a maximum age of 25 | |
3 => array( | |
'min' => 21, | |
'max' => 25, | |
), | |
// Level 4 requires a minimum age of 26 and a maximum age of 40 | |
4 => array( | |
'min' => 26, | |
'max' => 40, | |
), | |
// Level 5 requires a minimum age of 41 and a maximum age of 120 | |
5 => array( | |
'min' => 41, | |
'max' => 120, | |
), | |
); | |
// Get the level. | |
$level = pmpro_getLevelAtCheckout(); | |
$level_id = $level->id; | |
// Bail if things are not okay or if the level is not one that requires a birthday. | |
if ( ! $okay || ! array_key_exists( $level_id, $age_required_per_level ) ) { | |
return $okay; | |
} | |
// Get the birthday | |
if ( isset( $_REQUEST['birthday'] ) && ! empty( $_REQUEST['birthday'] ) ) { | |
$birthday = $_REQUEST['birthday']; | |
} else { | |
pmpro_setMessage( 'Please fill out the Birthday field.', 'pmpro_error' ); | |
return false; | |
} | |
// Check if the birthday values are numeric and is a valid date. | |
if ( ! is_numeric( $birthday['m'] ) || ! is_numeric( $birthday['d'] ) || ! is_numeric( $birthday['y'] ) || ! checkdate( $birthday['m'], $birthday['d'], $birthday['y'] ) ) { | |
pmpro_setMessage( 'Please enter a valid birthday.', 'pmpro_error' ); | |
return false; | |
} | |
$now = new DateTime(); // Get the current date. | |
$dob = new DateTime( $birthday['y'] . '-' . $birthday['m'] . '-' . $birthday['d'] ); // Get the date of birth. | |
// Calculate the age of the user. | |
$age = $dob->diff( $now )->y; | |
// Check if the user is within the required age range for the level. | |
if ( $age < $age_required_per_level[ $level_id ]['min'] || $age > $age_required_per_level[ $level_id ]['max'] ) { | |
pmpro_setMessage( 'You must be between ' . $age_required_per_level[ $level_id ]['min'] . ' and ' . $age_required_per_level[ $level_id ]['max'] . ' years old to register for this level.', 'pmpro_error' ); | |
return false; | |
} | |
return $okay; | |
} | |
add_filter( 'pmpro_registration_checks', 'my_pmpro_required_age_range' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment