In this quick tutorial we will learn about allowing or disallowing WordPress user registration from custom TLDs and domain extensions for the user provided email.

You can either choose a set of TLDs and domain extensions to only limit and restrict registration to, or add a list of TLDs to prevent while processing user registration filtering the errors.

There is already Restrict User Registration free WordPress plugin which “Allows you to restrict registration for custom usernames, email addresses and custom email service providers”. This can be handy for denying registrations from specific email addresses or even email domains, so it is useful for this purpose.

Allowing registrations from custom TLDs:

In the following code, make sure to specify the extensions you want user registration to be restricted to, and place them into the array:

$allowed_tlds = array( "be", "nl" ); // allowing Belgium and Netherlands emails

Now as long as my email address is from a BE or NL email service provider site, then I am able to register.

Disallowing registrations from custom TLDs:

This process instead will deny registering users with specific TLDs while leaving the rest allowed. To use this, just add few extensions into the following array in follows-code.

$forbidden_tlds = array();

The code:

After you make your necessary edits, place this code into your child theme’s functions file, or with a custom plugin:

/**
* Plugin Name: Restrict mail TLD registration
* Plugin URI:  http://samelh.com
* Description: Allow/disallow user registration for email TLD
* Author:      Samuel Elh
* Author URI:  http://samelh.com
* Version:     0.1
*/

function se__verify_tld( $email ) {

	/* enter the TLDs to allow into following array, e.g array( "be", "nl", "cr" ) in lowercase */
	$allowed_tlds = array( "be", "nl" ); // allowing Belgium and Netherlands emails

	/* OR, enter few TLDs to prevent (forbidden) in lowercase */
	$forbidden_tlds = array();

	if ( empty( $email ) ) return false;
	$tld = strtolower( substr( $email, strpos( $email, '.' )+1 ) );
	if ( empty( $tld ) ) return true; // no tld caught, trigger error
	else $tld = strtolower( $tld );
	
	if ( !empty( $forbidden_tlds ) ) {
		if ( in_array($tld, $forbidden_tlds) ) {
			return true;
		}
	}

	else if ( !in_array($tld, $allowed_tlds) ) {
		return true;
	}

	return false;

}

add_filter( 'registration_errors', function( $errors, $user_login, $user_email ) {

	if ( se__verify_tld( $user_email ) ) {
		$errors->add( "tld_exception", "Sorry, you can not sign up with emails from this TLD" );
	}

    return $errors;

}, 10, 3 );

preview - Allowdisallow user registration for email TLD

Link to Github gist (downloadable plugin)

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit