• Resolved cjnash

    (@cjnash)


    Hello!

    I am having a bug with one of my plugins on my site and have traced the cause to the fact that users are creating usernames with spaces in them. WordPress handles this fine, but my plugin does not, unfortunately. I cannot find a way to restrict the usernames to alphanumeric characters only with Ultimate Member. Does anybody know if this can be done?

    Thanks!

    Chris

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 16 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @cjnash

    Did you choose the “Unique Username” as a validation for the Username field?
    https://drive.google.com/file/d/1Qi6mHEq5A-hjx_cL7FLCzvpzYG1K-ZiQ/view

    I tried entering username with spaces and it triggers an error( UM version 2.2.5 ): https://drive.google.com/file/d/1nHlCsTjXlmDUcbFojfJYcYv8Wc-f5_co/view

    Regards,

    Thread Starter cjnash

    (@cjnash)

    Hi @champsupertramp

    I did select that option. Here’s what my form looks like:

    View post on imgur.com

    Oddly, yours is functioning exactly how I would like mine to work!

    Chris

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi,

    Can you do a conflict test and see if the issue goes away? This doc explains how to do a conflict test if you are not sure: How to do a plugin/theme conflict test?

    Regards,

    Hi @champsupertramp and @cjnash

    I join the question, the same thing happens with the user field.

    Field validation is enabled on unique username and it still allows spaces and that causes unexpected logout to occur

    Thread Starter cjnash

    (@cjnash)

    @champsupertramp Thanks for the advice! I will probably have the chance to do that early next week.

    Thanks for your help!

    Hello again @champsupertramp

    I need to be able to restrict spaces in usernames urgently, as the “unique username” field option seems to not work well. I need some solution, even if it is manual.

    Please…

    @srwayne

    Which version of UM?

    @srwayne and @cjnash

    This code snippet should be your solution:

    add_filter ( 'sanitize_user', 'my_um_remove_user_spaces', 10, 3);
    
    function my_um_remove_user_spaces( $username, $raw_username, $strict = false ) {
    
        return str_replace( ' ', '', $username );
    }

    Install the code snippet in your child-theme functions.php file
    or use the “Code Snippets” plugin.

    https://wordpress.org/plugins/code-snippets/

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hey there!

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. 🙂

    Regards,

    I do not want to allow the user to give space in the username, tell any code for that

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @khann1

    Have you tried the code above provided by @missveronicatv ?

    Regards.

    That is not working

    @khann1

    It’s working and the registered username is without spaces without any message about the change of the username.

    I have changed the code snippet now to stop the user from registering with spaces and there is an error message displayed.

    add_filter ( 'sanitize_user', 'my_um_remove_user_spaces', 10, 3);
    
    function my_um_remove_user_spaces( $username, $raw_username, $strict = false ) {
    
        if( strpos( trim( $username ), ' ' )) {
            UM()->form()->add_error( 'user_login', __( 'You are not allowed to have spaces in your username.', 'ultimate-member' ) );
        }
        return $username;
    }

    Note: I have made a correction as the return of the username was commented.
    This caused errors for all usernames being sanitized.

    • This reply was modified 4 years, 3 months ago by missveronica.
    • This reply was modified 4 years, 3 months ago by missveronica.
    • This reply was modified 4 years, 3 months ago by missveronica.

    @khann1

    I will make a second update of this code snippet.

    Now the test for space is done only for the registration page
    and if found spaces there is an error message.

    Old registrations with spaces
    which have been removed by the very first code snippet
    the user can use spaces at login
    and the username without spaces is used by UM and WP.

    add_filter ( 'sanitize_user', 'my_um_remove_user_spaces', 10, 3);
    
    function my_um_remove_user_spaces( $username, $raw_username, $strict = false ) {
        
        if( um_is_core_page( 'register' ) && strpos( trim( $username ), ' ' )) {
            UM()->form()->add_error( 'user_login', __( 'You are not allowed to have spaces in your username.', 'ultimate-member' ) );
        }
        return str_replace( ' ', '', $username );
    }
    • This reply was modified 4 years, 3 months ago by missveronica.

    I had solved this with jquery, I have another question, you have put a plugin of sponsor, now I want to show the profile of the sponsor whenever there is no sponsor, then that button should be hidden.

Viewing 15 replies - 1 through 15 (of 16 total)

The topic ‘Restrict usernames to not include spaces’ is closed to new replies.