Skip to:
Content
Pages
Categories
Search
Top
Bottom

Registration validation based on profile field


  • kida18
    Participant

    @kida18

    Hello!

    I would like to restrict the registration on my site to only a specific email domain, and only if the member type is X.
    My registration form includes the basic name/email/password fields as well as the buddypress base group fields, where the user can choose the member type (set up with plugin BuddyPress Xprofile Member Type Field).

    Browsing around I found the following code to validate the email domain:

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

    1) I tried replacing $valid_email_domains = array("gmail.com","yahoo.com"); with $valid_email_domains = (".edu"); but it didn’t work – I guess I should add a variable before it, to precise anything.edu as a valid domain? In a function, what would be the way to pass the “anything” argument? (I apologize if I don’t use the proper terms)
    Would it be “” ?

    2) I’ve tried checking how I could add one more validation argument, but I’m a bit lost.
    After checking around and in the register.php, I guess I should use something like :

    $membertype = $_POST ( 'type' => 'recruiter' )
     foreach( $membertype as $membertype ) {

    So, perhaps something like

    function is_valid_email_domain($login, $email, $membertype, $errors ){
     $membertype = $_POST ( 'type' => 'recruiter' )
     foreach( $membertype as $membertype ) {
    I don't know what to put here - how to link both arguments "if member type is "recruiter" and if email domain belong to the whitelist then proceed, if not give an error message"
        $valid_email_domains = (""".edu");// whitelist
        $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
        if( $valid === false ){
            $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using your .edu email address' ));
        }
    }
    
    add_action('register_post', 'is_valid_email_domain',10,3 );

    I’m probably completely wrong, I don’t know how to code, I’ve just tried putting several things I found together..but if someone has an idea on how I could make it work I would be very grateful

Viewing 4 replies - 1 through 4 (of 4 total)

  • snorklebum
    Participant

    @snorklebum

    function is_valid_email_domain($login, $email, $errors ){
     $valid_email_domain = ".edu";// whitelist email domain
     $valid = false;
     $current_email_domain = strtolower( substr( $email, -4));
     if( $current_email_domain == strtolower($valid_email_domain) ){
     $valid = true;
     break;
     }
     // if invalid, return error message
     if( $valid === false ){
     $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using .edu emails' ));
     }
    }
    add_action('register_post', 'is_valid_email_domain',10,3 );

    I’m a bit rusty with php but give that a try, as you’re only doing one lookup I’ve removed the array and loop.


    kida18
    Participant

    @kida18

    Thanks Snorklebum,

    I’ve tried but it didn’t work, I could register with a gmail address

    Would you have an idea about the other part, how to add another argument into the function?


    danbp
    Participant

    @danbp

    Hi @kida18,

    i would suggest to use this premium plugin:

    WordPress Restrict Email Domains


    For < 20$, i think it’s worth ! 🙂


    kida18
    Participant

    @kida18

    I thought about a plugin, but I’m using so many already! 🙂
    Plus, I need to limit the email domain registration only for one member type, not everyone, and I don’t see this option in the plugin.
    But thanks for the suggestion!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar