Allow Registration From Certain Email Domain – WordPress

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 whitelist 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 into 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 into 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


That’s all you need. If you see this article helpful then share it on facebook or twitter. thanks for reading this article. You can comment below.

 

One response to “Allow Registration From Certain Email Domain – WordPress”

  1. Vijay lathiya says:

    How we can do it for user registration by WooCommerce My Account page ?

Leave a Reply

Your email address will not be published. Required fields are marked *