• Resolved codings

    (@codings)


    Hi UM Team, thank you for this plugin!

    Is there a way to prevent new users from registering a username with spaces? With an error message telling them to use “username” and not “user name”?

    I couldn’t find a native option in Ultimate Member settings, so I tried this function:

    add_filter( 'validate_username' , 'custom_validate_username', 10, 2);

    add_filter('validate_username', function (bool $isValid, string $username): bool {
    if (preg_match('/[^a-z]/', $username)) {
    return false;
    }

    return $isValid;
    });

    Also tried this to prevent capital letters:


    add_filter( 'validate_username' , 'custom_validate_username', 10, 2);

    function custom_validate_username( $valid, $username ) {
    if ( preg_match("/\\s/", $username) ) {
    // There are spaces, return a WP_Error object with a custom message
    return new WP_Error( 'username_has_spaces', __( '<strong>Error</strong>: Usernames cannot contain spaces.', 'your-text-domain' ) );
    }
    return $valid;
    }

    Neither functions are working and I think it may be because UM is handling the registration. It seems like a good idea to have some control over what is allowed when a member creates a new username. Is there a way to achieve this?

Viewing 1 replies (of 1 total)
  • Thread Starter codings

    (@codings)

    Anyone wanting to prevent use of spaces in usernames, the above code is incorrect.

    The correct answer is here, with code below placed in the functions.php (I still think this would be a good option to put in the settings).

    add_action( 'um_submit_form_register', 'um_submit_form_register_no_spaces', 9, 1 );
    
    function um_submit_form_register_no_spaces( $args ) {
    
        if ( isset( $args['user_login']) && strpos( trim( $args['user_login'] ), ' ' ) > 0 ) {
            UM()->form()->add_error( 'user_login', __( 'You are not allowed to have spaces in your username.', 'ultimate-member' ) );
        }
    }

    UM Team, is there a way to add to that function to also prevent use of capitalization in usernames?

Viewing 1 replies (of 1 total)

You must be logged in to reply to this topic.