Restrict WordPress Registration to Certain Email Domains

Home/Restrict WordPress Registration to Certain Email Domains

Recently for a project it was necessary to restrict WordPress registration to a select few specific domains, for example: domains with an email address from a certain company, i.e. email addresses ending with @warrenchandler.com.

Rather than using a plugin to do limit domain access when registering, a few lines of code is all it takes to restrict access on sign-up, with a handy error message included to boot.

Here’s the code you’ll need – added to your site theme’s functions.php file.

// Allow Registration Only from @warrenchandler.com email addresses

function is_valid_email_domain($login, $email, $errors ){
 $valid_email_domains = array("warrenchandler.com","warrenchandler.co.uk");// 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>: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact the system administrator.' ));
 }
}
add_action('register_post', 'is_valid_email_domain',10,3 );

And that’s all it takes. Invalid email domains will produce the following result:

WordPress Registration from Certain Domains Only

2017-05-19T15:48:04+00:00 December 12th, 2016|WordPress Snippets|0 Comments

About the Author:

Warren Chandler is a freelance web programmer and web developer, based in Frinton on Sea, Essex. Warren specialises in web programming, web design, corporate identity, copy writing, SEO, logo and print media for home, work, and mobile platforms.

Leave A Comment