Restrict registration to email whitelist

Home Forums User Registration Restrict registration to email whitelist

This topic contains 3 replies, has 2 voices, and was last updated by  sanjeev3 12 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #6438

    Hello!

    How can i restrict registration by email from “whitelist”?

    will this code work? :

    add_action('registration_errors', 'sizeable_restrict_domains', 10, 3);
    function sizeable_restrict_domains( $errors, $login, $email ) {
    	$whitelist = array('sizeableinteractive.com', 'theandystratton.com');
    	if ( is_email($email) ) {
    		$parts = explode('@', $email);
    		$domain = $parts[count($parts)-1];
    		if ( !in_array(strtolower($domain), $whitelist) ) {
    			$errors->add('email_domain', __('ERROR: You may only register with an approved email address.'));
    		}
    	}
    	return $errors;
    }
    #6461

    sanjeev3
    Member

    Hello Nikolay,

    Please use provided user registration hook user_registration_validate_user_email instead. Paste the following codes to your child theme’s functions.php

    add_action( 'user_registration_validate_user_email','ur_validate_user_email', 10, 4 );
    
    function ur_validate_user_email( $single_form_field, $data, $filter_hook, $form_id ) {
    
    	$value 	   = isset( $data->value ) ? $data->value : '';
    	$whitelist = array( 'sizeableinteractive.com', 'theandystratton.com' );
    	$parts     = explode( '@', $value );
    
    	if( ! in_array( $parts[1], $whitelist ) ) {
    
    	    add_filter( $filter_hook, function ( $msg ) use ( $field_label ) {
    	        return __( 'You may only register with an approved email address.', 'user-registration' );
    	   });
    	}
    }

    Let me know if you have any further confusion.

    Regards,
    WPEverest Support

    #6474

    Thank you very much!

    One question more please – i want that restriction work not global, but only in UR form. Which file of plugin i need to modify (instead of functions.php)?

    ps. Sorry for bad english )

    #6554

    sanjeev3
    Member

    Hello nikolay.novikov,

    That is only for user registration form. It will not effect globally. Also, It is NOT recommended to edit the plugins file directly as all edited files will be replaced by the files in the plugin update. Only to let you know, the validation script should be here in the plugin: https://github.com/wpeverest/user-registration/blob/master/includes/form/class-ur-form-field-user-email.php#L60

    Thanks!

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

You must be logged in to reply to this topic.