· 9 years ago · Dec 02, 2016, 12:14 PM
1function is_valid_email_domain($login, $email, $errors ){
2 $valid_email_domains = array("gmail.com","yahoo.com");// whitelist email domain lists
3 $valid = false;
4 foreach( $valid_email_domains as $d ){
5 $d_length = strlen( $d );
6 $current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
7 if( $current_email_domain == strtolower($d) ){
8 $valid = true;
9 break;
10 }
11 }
12 // if invalid, return error message
13 if( $valid === false ){
14 $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: you can only register using @gmail.com or @yahoo.com emails' ));
15 }
16 }
17 add_action('register_post', 'is_valid_email_domain',10,3 );
18
19<?php
20/**
21 * Handles/processes registration forms data.
22 */
23
24add_action( 'user_register', '_hrb_user_register_role' );
25
26/**
27 * Assigns the selected role to the user being registered.
28 */
29function _hrb_user_register_role( $user_id ) {
30
31 if ( empty( $_POST['role'] ) ) {
32 return;
33 }
34
35 $valid_roles = array_keys( hrb_roles() );
36 $role = $_POST['role'];
37
38 // make sure we always get a valid role on registration
39 if ( empty( $role ) || ! in_array( $role, $valid_roles ) ) {
40 $role = HRB_ROLE_BOTH;
41 }
42
43 wp_update_user( array ( 'ID' => $user_id, 'role' => $role ) );