skip to Main Content

Other email address should not be able to register on my website registration page except for emails ending in "@domain.com"

2

Answers


  1. You can use this code in your theme functions.php file. Just change the domain.com to something else like gmail.com,outlook.com etc

    // Custom domain registration only
    
    function custom_domain_email($login, $email, $errors ){
     $accepted_domains_emails = array("domain.com");// allowed domains
     $valid = false; // sets default validation to false
     foreach( $accepted_domains_emails as $d ){
      $d_length = strlen( $d );
      $accepted_email_domain = strtolower( substr( $email, -($d_length), $d_length));
     if( $accepted_email_domain == strtolower($d) ){
      $valid = true;
      break;
     }
     }
     // Show error message
     if( $valid === false ){
    
    $errors->add('domain_whitelist_error',__( 'Registration is only allowed from domains.com domain only.' ));
     }
    }
    add_action('register_post', 'custom_domain_email',10,3 );
    
    Login or Signup to reply.
  2. Easiest is to use a regular expression and match the given email address against it. Here is a simple demonstration using four addresses and deciding for each, whether it is accepted or rejected:

    <?php
    $emailAddresses = [
      "[email protected] ",
      "   [email protected]",
      "[email protected]", 
      "personD@ivalid",
      "personE"
    ];
    
    array_walk($emailAddresses, function($emailAddress) {
      var_dump(preg_match('/@good.com$/', trim($emailAddress)) ? "accepted" : "rejected");
    });
    

    The output obviously is:

    string(8) "accepted"
    string(8) "accepted"
    string(8) "rejected"
    string(8) "rejected"
    string(8) "rejected"
    

    So all you actually need is that command:

    preg_match(‘/@domain.com$/’, trim($emailAddress))

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search