phpwordpressregistrationultimate-member

Ultimate member custom username validation not working


I am using Ultimate Member version 2.6.11 on WordPress version 6.3.1. I am having trouble with my custom username validation code.

I am trying to validate the username field to make sure that it contains the string “1234”. I am using the following code:

add_action( 'um_submit_form_errors_hook', function( $args ) {
    if ( isset( $args['mode'] ) && 'register' === $args['mode'] ) {
        if ( !str_contains( $args['user_login'], '1234' ) ) {
            UM()->form()->add_error( 'user_login', __( 'Username must contain the string "1234".', 'ultimate-member' ) );
        }
    }
}, 8, 1 );

However, this code is not working. I am still able to register users with usernames that do not contain the required string. Can you please help me?

Thank you


Solution

  • I finally found a solution, the problem was the hook of the action. I changed it with um_submit_form_errors_hook__registration so that in the end my code actually looks like this:

    add_action( 'um_submit_form_errors_hook__registration', function( $submitted_data ) {
        
        if ( !str_contains( $submitted_data['user_login'], '1234' ) ) {
            UM()->form()->add_error( 'user_login', __( 'Username must contain the string "1234".', 'ultimate-member' ) );
        }
    }, 999, 1 );