Block Several Email Domains from WordPress Registration

WordPress Registration

There are many spammers out there trying to register account on WordPress site installation. We don’t know if the spam is a human or bot machine. There is no a perfect way for combating the spammers even by using CAPTCHA, email confirmation, logical questions, and many several process of registration functions. This tutorial will give a basic explanation on how to block several email domains from registering account on WordPress based site.

By default, WordPress points users to url “wp-login.php?action=register” for registering process. There are several actions and filters hook on “wp-login.php” file. We will use register_form to add custom text in the registration form and register_form action to create custom check for several domains.

add_action( 'register_form', 'codecheese_register_form' );

function codecheese_register_form() {  	
    echo '<p>' . __('Don\'t use email domain and!') . '</p><br />';

The function above will add custom paragraph to the registration form for ask visitors not to use email from blocked email domains.

add_action( 'register_post', 'codecheese_register_post', 10, 3 );

function codecheese_register_post( $sanitized_user_login, $user_email, $errors ) {
    // Blocked domains  
    $domains = array( '', '' );
    // Get visitor email domain
    $email = explode( '@', $user_email );
    // Check and display error message for the registration form if exists
    if( in_array( $email[1], $domains ) )
        $errors->add('invalid_email', __('ERROR: Please use another email.'));

From the function above, we can add all blocked domains in array values. The function will be hooked or run if there is no errors, username and email address is valid. The next process is by exploding the user email by exploding it, and check if it exists in the blocked domains array. If it exists, it will add error message and redirect users back to the registration form.

That’s a simple way for blocking several email domains. We can modify it to more complex way by running several checks or using other email checker API to check from the spams book. Have a nice coding day.

3 comments on “Block Several Email Domains from WordPress Registration

  1. Thank you very much for this! One of my sites were flooded with spam registrations a while ago, added some captcha and it was reduced considerably. But some yahoo emails continued to get trough.. I really hope this works, because it is already annoying 🙂 Will see the next few days

  2. ah and forgot to tell… it stops the “normal” registrations with the banned emails, I tried that. But not sure if the spammers do use the ‘normal ways’ and not some hidden paths …

Create Comment

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