plugins - Only allowing some email addresses to create an account

admin2025-06-02  1

I am creating a website that would need to only allow some specific email addresses to signup (for example "[email protected]"). Do you guys know of any wordpress email confirmation plugins that would allow me to do that?

To clear some confusion: What I am looking for is only allowing some email addresses (for example all emails ending in @unimail.co.uk), I don't want to have to write down all of them independently. I don't really know how to code php so it would be super helpful if someone could help. Once again, sorry for the confusion created with the first question.

Thank you

I am creating a website that would need to only allow some specific email addresses to signup (for example "[email protected]"). Do you guys know of any wordpress email confirmation plugins that would allow me to do that?

To clear some confusion: What I am looking for is only allowing some email addresses (for example all emails ending in @unimail.co.uk), I don't want to have to write down all of them independently. I don't really know how to code php so it would be super helpful if someone could help. Once again, sorry for the confusion created with the first question.

Thank you

Share Improve this question edited Mar 9, 2019 at 10:29 Afonso de Sousa asked Mar 6, 2019 at 11:13 Afonso de SousaAfonso de Sousa 113 bronze badges 3
  • If you wanted to develop this yourself, I think you need to hook register_post (called from register_new_user) and in your add_action callback add an error to the WP_Error object if the email address doesn't match you domain filter. – Rup Commented Mar 6, 2019 at 17:18
  • never use register_post to add custom validation. Use the filter "registration_errors" instead. Have a look at my answer ;) – HU is Sebastian Commented Mar 8, 2019 at 8:57
  • Tanmay's answer is a modified version of this code which does domain filtering, not individual emails, so you could just use that. Although as kuchenundkakao points out it's technically using the wrong hook according to a comment in the codex wiki (albeit one with no justification) but it should be easy to port to the other hook if you want, or modify k-u-k's answer to match domains. – Rup Commented Mar 11, 2019 at 11:41
Add a comment  | 

4 Answers 4

Reset to default 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 email addresses and Only allowing some email addresses to create an account. See the code below

function is_valid_emails($login, $email, $errors ){
     $valid_emails = array("[email protected]");
     $valid = false;
     foreach( $valid_emails as $valid_email ){
         $valid_emails_list = strtolower($valid_email);
         $current_email = strtolower($email);
         if( $current_email == $valid_emails_list ){
         $valid = true;
            break;
         }
     }
     if( $valid === false ){
        $errors->add('domain_whitelist_error',__( "<strong>ERROR</strong>: you can't register email because only allow some specific email addresses" ));
     }
    }
    add_action('register_post', 'is_valid_emails',10,3 );

You can add more email addresses by adding more inside the code:

$valid_emails = array("[email protected]","[email protected]");

Instead of using the register_post action, you should use the registration_errors filter like this (copied from codex, then modified):

function my_awesome_email_validation_filter( $errors, $sanitized_user_login, $user_email ) {
     $allowed_email_addresses = array('[email protected]','[email protected]');    //be sure to write all the allowed addresses in lowercase
     if(!in_array(mb_strotolower($user_email),$allowed_email_addresses)){
          $errors->add( 'email_not_allowed', __( '<strong>ERROR</strong>: Sorry, but you are not allowed to register.', 'my_textdomain' ) );
     }
    return $errors;
}

add_filter( 'registration_errors', 'my_awesome_email_validation_filter', 10, 3 );

Happy Coding!

https://wordpress/plugins/wp-approve-user/ Is a plugin that allows you to approve user registrations before they are allowed to login to your site. All user registrations essentially go into a Pending state until the admin user approves them.

The plugin woocommerce memberships does this. But it is pricey for just that feature. Better to use the code suggested above.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748827208a314051.html

最新回复(0)