Last active
October 6, 2022 19:34
-
-
Save yuriinalivaiko/672c5885670d9e236284edf48f8d7442 to your computer and use it in GitHub Desktop.
This code snippet restricts registration for under 18 years old using the custom validation applied to the Birth Date field. You can add this code to the functions.php file in the active theme directory.
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 | |
/** | |
* Restrict registration for under 18 years old. | |
* | |
* @param array $args Form Arguments. | |
*/ | |
function um_custom_validate_birth_date( $args ) { | |
if ( ! empty( $args['birth_date'] ) ) { | |
// Birth date as a Unix timestamp. | |
$then = strtotime( $args['birth_date'] ); | |
// A person 18'th birthday as a Unix timestamp. | |
$adulthood = strtotime( '+18 years', $then ); | |
// Current time. | |
$now = time(); | |
if ( $now < $adulthood ) { | |
UM()->form()->add_error( 'birth_date', __( 'You should be over 17 years old.', 'ultimate-member' ) ); | |
} | |
} | |
} | |
add_action( 'um_submit_form_errors_hook_', 'um_custom_validate_birth_date', 30, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is a part of the article Restrict registration for under 18 years old
Documentation: https://docs.ultimatemember.com/
Support forum: https://wordpress.org/support/plugin/ultimate-member/