Skip to content

Instantly share code, notes, and snippets.

@yuriinalivaiko
Last active October 6, 2022 19:34
Show Gist options
  • Save yuriinalivaiko/672c5885670d9e236284edf48f8d7442 to your computer and use it in GitHub Desktop.
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.
<?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 );
@yuriinalivaiko
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment