Allow registration for certain email domain on WordPress

By informerfrk

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

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("gmail.com","yahoo.com");// 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

Tags:

Leave a Reply

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