2 January 2014

Email Restrictions For Registration In WordPress

Here is a way to restrict registration to a specific email domain using following WordPress hook.
You can add more domains to restrict list by adding domain into $blockDomains. and also you can edit your custom error message(By changing $error_message)...

Add following code in your function.php file.
add_action('register_post', 'email_restrictions_on_registration', 10, 3);
function email_restrictions_on_registration( $login, $email, $errors )
{
 // Add those specific email domain in '$blockDomains' array.
 $blockDomains = array('gmail.com', 'domain.com', 'domain-name.com');
 //Add Your custom error message here.
 $error_message = "Only Email Addresses From Approved Domains Are Allowed!";
 //Split email address with @ to get domain name.
 $email_data = explode('@', $email);
 $username = $email_data[0];
 $domain = $email_data[1];
 //Check if domain is in block Domain list.
 if(in_array($domain, $blockDomains))
 {
  $errors->add(
   'invalid_email',
   __("<strong>ERROR</strong>: $error_message"),
   array('form-field' => 'user_email')
  );
 }
}

2 comments:

  1. Do we put this in the function.php?

    ReplyDelete
  2. Yes of course...Put above code in "function.php" file...

    ReplyDelete