Snippets · Wordpress

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_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')
		);
	}
}

5 thoughts on “Email Restrictions For Registration In WordPress

  1. Pretty nice post. I just stumbled upon your blog and
    wanted to say that I have truly enjoyed browsing your blog posts.
    In any case I’ll be subscribing to your feed and I hope you write again soon!

Leave a comment