• Hi there,

    I may have stumbled upon a bug in the wordpress system. I have a script were i need to check the username before adding it with wp_insert_user.

    In wp_users i have a user with a user_login value of: Cafe t test

    When i try to insert the following username in wp_insert_user the wp_error tells me that the username exists already: Café ‘t test
    That seems to be correct, so i need to check the value before adding with username_exists(). But that function returns null.. ?
    When i remove the accents and the quotes both functions tell me the username exists. But in my opinion, the username_exists functionn should do that by default.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Accents are stripped from login names. It is part of the sanitize_user() function that is called by both username_exists() and wp_insert_user(). The problem is the 't portion. The single quote is a separate character, not an accent. Whether this character is stripped by sanitize_user() depends on if the ‘$strict’ parameter is provided as true or not. wp_insert_user() uses the strict version. username_exists() does not. The fault of failing to use the strict version is actually in the WP_User::get_data_by() method which is called by username_exists().

    I agree the inconsistent application of the strict mode is inappropriate, but it is what it is. You can see if anyone has reported this issue with a core.trac search. If not, consider reporting a bug. In the mean time you should use username_exists() like so, even though it is mostly redundant:
    username_exists(sanitize_user($login, true))

    There is also another gotcha with using username_exists() vs wp_insert_user(). If you try to check the username ??????? for example, if you use username_exists(sanitize_user($login,true)), you will get null, but if you try to use wp_insert_user, you will get an error.

    This can be an issue if you are importing users from a system that allows special characters. The way I got around that is to use:

    if (username_exists(sanitize_user($login,true)) || '' == sanitize_user($login,true)) {
    // do not add user
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘username_exists does not work with special chars’ is closed to new replies.