Block or allow domain extensions to register

Sup guys!

At the moment I’m trying to figure out if there is an excising plugin or a way to create a list to block/allow domain name extensions that comes with people’s emails they use for registration. Or simple said: only allow registration when using an email address that ends with .co.nz, blocks .com but allow @hotmail.com and @gmail.com. And block every other kind of email address.

I know there is a plugin called User Domain Whitelist but it didnt give me the result I wanted, stated above.

Anybody some suggestions or feedback on this?

Thanks in advance,

Ronald

  • Alexander
    • DEV MAN’s Mascot

    Hello @dutchman,

    I took a look around and tried to find something but didn’t come up with anything better than the plugin you mentioned. I also see how it’s pretty much tied to an entire domain, instead of allowing for partial matches.

    So, I put together the snippet below for you. You can include it in a plugin or a theme’s functions.php

    add_action('registration_errors', 'validate_email_domain', 10, 3);
    function validate_email_domain( $errors, $login, $email ) {
    $expression = "/.*@(.*.co.nz|hotmail.com|gmail.com)/";

    if ( is_email($email) ) {
    if ( !preg_match($expression,$email) ) {
    $errors->add('email_domain', __('ERROR: You may only register with an approved email address.'));
    }
    }
    return $errors;
    }

    I’ve tested it, and it will match gmail.com, hotmail.com, and *.co.nz domains. If you need help modifying it, let me know! Hope this helps!