• Resolved treetex

    (@treetex)


    Hi,

    I’m trying to get this snippet working, but nothing happens. A user can still register with a username that contains spaces, e.g John Doe.

    add_filter( 'validate_username', 'custom_validate_username', 10, 2 );
    
    function custom_validate_username( $valid, $username ) {
    
        if ( preg_match( '/\s/', $username ) ) {
            return false;
        }
    
        return $valid;
    }


    Cleared server Varnish cache.

    Regards
    Jack

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi Jack,

    Thank you for writing in.

    We checked the information you shared and were able to confirm the behavior.

    As a temporary solution, please use the following code snippet to prevent usernames containing spaces. We have also forwarded this to our development team, and we will review it further and take the necessary steps in an upcoming update.

    add_action( 'user_registration_validate_user_login', 'custom_ur_validate_username_no_spaces', 10, 4 );
    
    function custom_ur_validate_username_no_spaces( $single_form_field, $data, $filter_hook, $form_id ) {
        $username = isset( $data->value ) ? $data->value : '';
        $label    = $single_form_field->general_setting->field_name;
    
        if ( preg_match( '/\s/', $username ) ) {
            add_filter(
                $filter_hook,
                function () use ( $label ) {
                    return array(
                        $label       => __( 'Username cannot contain spaces.', 'user-registration' ),
                        'individual' => true,
                    );
                }
            );
        }
    }
    

    Please give it a try and let us know if you need further assistance.

    Best regards!

    Thread Starter treetex

    (@treetex)

    Thx, that works!

    Best regards
    Jack

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.