· 10 years ago · Aug 22, 2016, 08:04 AM
1function wpse27756_restrict_register_email( $user_email )
2{
3 $errors = new WP_Error();
4
5 if ( ! preg_match( "/gmail/i", $user_email )
6 {
7 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: You are not allowed to use other mail accounts than GMail.' ) );
8 }
9 return $errors;
10}
11function wpse27756_add_register_email_filter( $user_email )
12{
13 add_filter( 'user_registration_email', 'wpse27756_restrict_register_email' );
14}
15add_action( 'init', 'wpse27756_add_register_email_filter' );
16
17function is_valid_email_domain($login, $email, $errors ){
18 $valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists
19 $valid = false;
20 foreach( $valid_email_domains as $d ){
21 $d_length = strlen( $d );
22 $current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
23 if( $current_email_domain == strtolower($d) ){
24 $valid = true;
25 break;
26 }
27 }
28 // if invalid, return error message
29 if( $valid === false ){
30 $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using @gmail.com or @yahoo.com emails' ));
31 }
32}
33add_action('register_post', 'is_valid_email_domain',10,3 );