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

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 ;) 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

4 Answers 4

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]");
1
  • Did you read the Codex page? "Please note that this hook should never be used for custom validation. Any custom validation rules should be performed using the registration_errors filter. " Commented Mar 8, 2019 at 8:50
1

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!

3
  • Why is that hook better? The comment on the hook does mention email validation errors but it doesn't read clearly to me that this hook is the place to add them.
    – Rup
    Commented Mar 8, 2019 at 10:33
  • Codex Page of register_post: "Please note that this hook should never be used for custom validation. Any custom validation rules should be performed using the registration_errors filter. " Commented Mar 8, 2019 at 15:02
  • Hmm, OK. There's no justification for that, nor is there documentation in the code itself: the codex wiki is the only place I can find that. So I'm not sure how authoritative it is. I guess it makes sense to standardise on everyone using one or the other, though.
    – Rup
    Commented Mar 8, 2019 at 15:14
0

https://wordpress.org/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.

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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