0

I am creating a website that would need to only allow some specific email addresses to signup .

An example of this would allowing all emails ending in "@uniname.ac.uk" but not allowing any "@gmail.com, @hotmail.com, etc...".

Do you guys know of any WordPress email confirmation plugins or php code that would allow me to do that?

Finally, I don't really know how to code php so it would be super helpful if someone could help.

Thank you

1

Just copy the above code and paste it into your theme’s functions.php file. Here I am going to show you the code which will reject registration from all others domain's email addresses and Only allowing @uniname.ac.uk email addresses to create an account. See the code below

function is_valid_email_domain($login, $email, $errors ){
    $valid_email_domain = array("uniname.ac.uk");
    $valid = false;
    foreach( $valid_email_domain 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( $valid === false ){
        $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using @uniname.ac.uk emails' ));
    }
}
add_action('register_post', 'is_valid_email_domain',10,3 );
1

Requirements

In Settings->General, the box Anyone can register must be checked. Email address entered in Email field of registration form, must have an email domain equal to '@uniname.ac.uk'

Email address Validation

To validate email address, we can use 'registration_errors' filter hook.

Code

Insert the following code into functions.php of your active theme (child theme, if exists):

function wpse_check_email_domain($errors, $sanitized_user_login, $user_email) {
    $start = strpos(strtolower($user_email), '@uniname.ac.uk');
    if(!$start)
        $errors->add('ERROR', 'Only users with "@uniname.ac.uk" email domain can register!');
    return $errors;
}
add_filter('registration_errors', 'wpse_check_email_domain', 10, 3);

Use it as a must use plugin

The code above is not really theme related. Use it in functions.php, for testing only. After testing, remove it from functions.php, create a php script (eg. domain-check.php), put the code in it (do not forget <?php as a first line), and save this script in 'wp-content/mu-plugins folder.

Explanation

After registration form submission, the hook 'registration_errors' will be triggered. The callback function 'wpse_check_email_domain' will be executed, and will return an error, if email domain is incorrect. The error message will appear on registration form. If there are no errors, registration process will continue.

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