Skip to content

Only allow WordPress Registration from specific Emails Domain Function

If you are suffering from registration spam and you have tried just about everything to stop it, only allowing WordPress Registration from specific Emails Function might be your solution. I Just got tired of constant Registration spam no matter what plugin I used to stop it Captcha and all nothing work. This is the only thing that worked for me. Hey anyone can get a free Gmail account 🙂
This is the Function which you put in your Theme function.php file:

function is_valid_email_domain($login, $email, $errors ){
 $valid_email_domains = array("gmail.com","aol.com");// allowed domains
 $valid = false; // sets default validation to 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;
 }
 }
 // Return error message for invalid domains
 if( $valid === false ){

$errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: You can only use Gmail or Aol for registration.' ));
 }
}
add_action('register_post', 'is_valid_email_domain',10,3 );

Just add which ever email domain you want to allow at the $valid_email_domains = array( line

Updated 7-13-21: Although it slow spam, those MF still found a way to register, so is not worth it to be honest if you are looking to stop spam registration.

Leave a Comment

You must Register or Login to comment on Only allow WordPress Registration from specific Emails Domain Function