Allow Registration From Certain Email Domain – WordPress

By F.A Mollick

Share on

WorPress-registration-email-restrict

Many times WordPress site owners can see that users are registering on their site using some different kinds of email and surely it is not like that popular types of email usually users use to register on a site. After installing some security plugins also you will see the problem. They are registering on your site for spamming purpose and you want to stop these types of registration.

Well, I have a solution for you. You can create some email domain white-list so that users or visitors can register on your site only using those emails that you want. I will show you in this post how to allow registration only using gmail and yahoo mail. All other types of email registration will be rejected. Thus you will get real registered members on your WordPress site and they are not from any spam email.

So how to do it ?

Here I am going to show you the code which will reject registration from all others email domain and only allow sign up from gmail and yahoo email. See the code below

 

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 );

 

Just copy the above code and paste it inside your theme’s functions.php file. If functions.php not exists that create one. Or you can also do it by creating a plugin and paste it inside the plugin’s PHP file. After adding this code trying yourself. Try to register with any other email and you will get the error message code. Registration will allow only using gmail and yahoo mail. You can add more email domain by adding more inside the code:

 $valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists


Thats all you need. If you see this article helpfull than share it on facebook or twitter. thanks for reading this article. You can comment below.

 

Comments are closed.