Last active
December 7, 2023 12:19
-
-
Save denisbaranov/27b74695ed671cb286a3efb48aa6aba6 to your computer and use it in GitHub Desktop.
The following code requires @gmail.com as an email domain for user registrations. You can change @gmail.com to any provider you want. The code below requires a user email to be collected during registration.
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 | |
/** | |
* This example shows how to validate the email address by domain on registration. | |
* The code below requires a user email to be collected during registration. | |
* | |
* Change the array $allowed_email_domains - add or remove what you need. | |
* Add this code snippet to the end of the file functions.php in the active theme directory. | |
* | |
* Ultimate Member documentation: https://docs.ultimatemember.com/ | |
* Ultimate Member support (for customers): https://ultimatemember.com/support/ticket/ | |
*/ | |
/** | |
* Custom validation for the field "E-mail Address" by domain on registration. | |
* @author Ultimate Member support <support@ultimatemember.com> | |
* @param array $args | |
*/ | |
function um_validate_email_domain( $args ) { | |
// Change allowed email domains here | |
$allowed_email_domains = apply_filters( 'um_allowed_email_domains', array( | |
'gmail.com', | |
'yahoo.com', | |
'hotmail.com' | |
) ); | |
// Change error message here | |
$message = __( 'You can not use this email domain for registration', 'ultimate-member' ); | |
if ( isset( $args['user_email'] ) && is_email( $args['user_email'] ) ) { | |
$email_domain = array_pop( explode( '@', trim( $args['user_email'] ) ) ); | |
if ( !in_array( $email_domain, $allowed_email_domains ) ) { | |
UM()->form()->add_error( 'user_email', $message ); | |
} | |
} | |
} | |
add_action( 'um_submit_form_errors_hook__registration', 'um_validate_email_domain', 20 ); |
Tried wildcard in the array, no luck.
function my_submit_form_errors_registration( $args ) {
// Change error message here
$message = __( 'You can not use this email domain for registration. Please ensure your department has been whitelisted by the staff.', 'ultimate-member' );
if ( isset( $args['user_email'] ) && is_email( $args['user_email'] ) ) {
$email_domain = strtolower( array_pop( explode( '@', trim( $args['user_email'] ) ) ) );
$pattern = '/^[a-zA-Z0-9\-\.]+\.(gov|org)$/i';
if ( !preg_match( $pattern, $email_domain ) ) {
UM()->form()->add_error( 'user_email', $message );
}
}
}
add_action( 'um_submit_form_errors_hook__registration', 'my_submit_form_errors_registration', 10, 1 );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a way to restrict this not to domains, but to specific TLD's? .gov and .org are needed on a project I am working on.