How to limit user registration to a specific set of domains?

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?

Related posts

Leave a Reply

2 comments

  1. 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/

  2. 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…