I’d like to limit registration to some of my sites based on the email domain of the user. For example, I only want people with @gmail.com
emails to register.
How would I do this?
2 s
You can do it easily by writing a code inside your theme’s functions.php file. here is the code:
function is_valid_email_domain($login, $email, $errors ){
$valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists
$valid = false;
foreach( $valid_email_domains as $d ){
$d_length = strlen( $d );
$current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
if( $current_email_domain == strtolower($d) ){
$valid = true;
break;
}
}
// if invalid, return error message
if( $valid === false ){
$errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using @gmail.com or @yahoo.com emails' ));
}
}
add_action('register_post', 'is_valid_email_domain',10,3 );
Source: https://www.eyeswift.com/allow-registration-from-certain-email-domain-wordpress/