• Resolved X-Raym

    (@x-raym)


    Hi !

    I’m trying to filter email address at register (with the register shortcode) if it doesn’t match certain domain names extensions.

    This seems to work fine on native register form,

    // Block certain email user registration domain extension
    add_filter( 'registration_errors', 'myplugin_check_fields', 10, 3 );
    function myplugin_check_fields( $errors, $sanitized_user_login, $user_email ) {
    
        $retval = preg_match( '/(.+)@(\S+)\.(\w+)/', $user_email, $matches );
        
        $allowed = ["fr", "com", "net"];
        
        if( $retval !== 1 || !isset($matches[3]) || !in_array($matches[3], $allowed ) ) {
            $errors->add( 'demo_error', __( '<strong>ERROR</strong>: User email domain extension not allowed.', 'my_textdomain' ) );
            $errors->add_data( 'demo_error', __( '<strong>ERROR</strong>: User email domain extension not allowed.', 'my_textdomain' ) );
        }
        
        return $errors;
    }

    (elaborated from https://codex.wordpress.org/Plugin_API/Filter_Reference/registration_errors )

    But not on the one generated by this plugin. It is as if the filter wasn’t triggered.

    Is registration_errors hook triggered bu this plugin ?

    Thx for your support !

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @x-raym,

    Yes, we do not trigger code hooks. We have our own hook to validate fields. Please use the following code for your purpose.

    function ur_validate_email_field( $single_form_field, $data, $filter_hook, $form_id ) {
    
    	if ( absint( $form_id ) === 1 ) { // Your form ID to restrict.
    
    		$user_email      = strtolower( $data->value );
    		$retval  = preg_match( '/(.+)@(\S+)\.(\w+)/', $user_email, $matches );
    		$allowed = [ 'fr', 'com', 'net' ];
    
    		if ( $retval !== 1 || ! isset( $matches[3] ) || ! in_array( $matches[3], $allowed ) ) {
    			add_filter(
    				$filter_hook,
    				function ( $msg ) {
    					return __( 'User email domain extension not allowed.', 'user-registration' );
    				}
    			);
    		}
    	}
    }
    add_action( 'user_registration_validate_user_email', 'ur_validate_email_field', 10, 4 );

    Regards!

    Thread Starter X-Raym

    (@x-raym)

    @rumesh38 Many thanks for the quick answer and updated code snippet !

    I’m curious though, why not using native hooks ?
    It makes porting from my old registration plugin to this one a bit more complex,
    and it can bring extensibility down by not allowing other plugin to hook regular filters. :/

    At least we have a workarround sure but extensibility ane interoperability are core of WordPress.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Q: is registration_errors not triggered ?’ is closed to new replies.