Registration validation based on profile field
-
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
- You must be logged in to reply to this topic.