Allow registration for certain email domain on WordPress

I have a WordPress blog where I found many users was creating their account on my WordPress site using different types of email which were unrecognized. Most of the people use Gmail, yahoo email. So most of the registered users should register with these email providers. But I was shocked, how most of the users registering on my WordPress site using that types of unknown email.

My WordPress site is secured with CloudFlare and Google reCaptcha plugin. I thought how is it possible to attack my website with boat and other types of malware. Well, the registered users from unknown email were not too much but obviously more than the users that are using Gmail.

After searching a lot on the internet I found the solution that I can take against registration from unrecognized emails. I found the code which allowed me to restrict the registration on my WordPress site by email. here is the code:

function is_valid_email_domain($login, $email, $errors ){
 $valid_email_domains = array("","");// 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;
 // if invalid, return error message
 if( $valid === false ){
 $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using or emails' ));
add_action('register_post', 'is_valid_email_domain',10,3 );

I just placed these code inside my theme’s functions.php file and it was working nicely. You can also create a separate plugin for this code and use it as a plugin.

Now look at the second line of the above code:

$valid_email_domains = array("","");// whitelist email domain lists

You will see where to list the email domain for whitelist those email which you want users to be able to register on your WordPress site. You can list more email and those email domain which you want to be allowed for user registration on your WP site. Just place those email domain in the same manner by comma separated and inside the quotation.

That all you need to do for allowing users to register on your WP site only from certain email providers. If you think this article helpful then please share it and also let me know through the comment.

Source: Allow Registration From Certain Email Domain – WordPress

4 responses to “Allow registration for certain email domain on WordPress”

  1. Naeem says:

    Thanks for this blog and code! It helped me a lot! Thank you so much!

  2. Joan says:

    Hi, I am trying to do something similar but I want to use the whitelist to give a higher role in WordPress, not to allow or disallow.
    User on whitelist will have a role and the once out of the whitelist will have another role. Is it possible? Could you help me?

  3. Josh says:

    I tried but it won’t work when using custom registration form

  4. Wayne says:

    Does NOT work for Woocommerce My Account page Registration

Leave a Reply

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