6

I'd like to limit registration to some of my sites based on the email domain of the user. For example, I only want people with @gmail.com emails to register.

How would I do this?

1
  • 1
    I've been working with the Log in with Google plugin from rtCamp to use Google credentials for access. Quite a simple solution, from a reputable source, and covering your needs. It also has the ability to whitelist domains. Commented Jan 8, 2021 at 11:29

2 Answers 2

3

You can do it easily by writing a code inside your theme's functions.php file. here is the code:

    function is_valid_email_domain($login, $email, $errors ){
 $valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists
 $valid = false;
 foreach( $valid_email_domains as $d ){
 $d_length = strlen( $d );
 $current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
 if( $current_email_domain == strtolower($d) ){
 $valid = true;
 break;
 }
 }
 // if invalid, return error message
 if( $valid === false ){
 $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using @gmail.com or @yahoo.com emails' ));
 }
}
add_action('register_post', 'is_valid_email_domain',10,3 );

Source: https://www.eyeswift.com/allow-registration-from-certain-email-domain-wordpress/

3

Tough task...

function wpse27756_restrict_register_email( $user_email ) {
    $errors = new WP_Error();

    if ( ! preg_match( "/gmail/i", $user_email ) {
        $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: You are not allowed to use other mail accounts than Gmail.' ) );
    }
    return $errors;
}

function wpse27756_add_register_email_filter( $user_email ) {
    add_filter( 'user_registration_email', 'wpse27756_restrict_register_email' );
}
add_action( 'init', 'wpse27756_add_register_email_filter' );

Just written straight out of my head without testing. Give it a try and let us know...

6
  • Great - thanks! I'll give it a try soon and see what happens - I'm actually not trying to limit to gmail users only (I want to limit to a specific .edu domain). but I'm sure if I change "gmail" to the specific .edu it should work? Has anyone tried using wordpress.org/extend/plugins/user-domain-whitelist ?
    – BentCircle
    Commented Sep 6, 2011 at 1:03
  • No, I haven't tried. I merely use plugins. If the above doesn't work with a simply replace of gmail, please go to stackoverflow.com and ask for advice for the preg_match. I'm pretty bad with regular expressions.
    – kaiser
    Commented Sep 6, 2011 at 11:59
  • thanks Kaiser - you mentioned plugins, are you using any plugin for this type of function? much appreciated
    – BentCircle
    Commented Sep 7, 2011 at 0:52
  • 1
    No, you got me wrong. I only use plugins for really big tasks, which I can't acchieve myself in the given time. If the above works for you, mark is as solution. You asked "limit to @gmail" and that's what it does - if you need more than that, please switch to stackoverflow as this would be the place inside the network where a Q about preg_replace should be asked.
    – kaiser
    Commented Sep 7, 2011 at 8:44
  • 1
    Thanks for the clarification. I'll test and let you know.
    – BentCircle
    Commented Sep 7, 2011 at 10:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.