15

I would love to be able to use spaces in usernames, but this does not seem possible. Is there a writeup in the codex or somewhere that explains what characters are allowed and why it was restricted to that set or is it just alphas only and suck it up?

If I want users to use something other than a username with no spaces, is my only real option a plugin that allows users to login with their email addresses instead, but still requires an alpha only username at account creation?

Oh, and I am on a WPMU/network site.

0

2 Answers 2

10

You can use spaces in usernames, no problem. Several users on wordpress.org have spaces in their usernames.

Strict mode only allows these characters: a-z0-9<space>_.\-@

However WP doesn't default to strict mode.

Now, multisite has different restrictions, and it may strip spaces there. This is because usernames are used to create independent blogs and such on multisite installs.

2
  • Makes sense, and yet my multi install will not allow me to create users with spaces, @, or . in the name. I looked at formatting.php and it does appear to allow @ . and spaces. Any idea of where I should search to figure out what is preventing these characters? I would just like to know why they are prevented.
    – Purcell
    Commented Jul 5, 2011 at 20:20
  • 4
    It's in the function wpmu_validate_user_signup in wp-includes/ms-functions.php, and yes, it only allows letters and numbers. Definitely a multisite thing.
    – Otto
    Commented Jul 5, 2011 at 21:05
5

I don't think there's any official documentation about that, but you can have a look at the sanitize_user function in wp-includes/formatting.php :

function sanitize_user( $username, $strict = false ) {
    $raw_username = $username;
    $username = wp_strip_all_tags( $username );
    $username = remove_accents( $username );
    // Kill octets
    $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
    $username = preg_replace( '/&.+?;/', '', $username ); // Kill entities

    // If strict, reduce to ASCII for max portability.
    if ( $strict )
        $username = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $username );

    $username = trim( $username );
    // Consolidate contiguous whitespace
    $username = preg_replace( '|\s+|', ' ', $username );

    return apply_filters( 'sanitize_user', $username, $raw_username, $strict );
}

You could hook into that function and override the default behavior with your own.

1
  • Thanks for pointing out formatting.php. My multi install will not allow me to create users with spaces, @, or . in the name. I looked at formatting.php and it does appear to allow @ . and spaces. Any idea of where I should search to figure out what is preventing these characters? I would just like to know why they are prevented.
    – Purcell
    Commented Jul 5, 2011 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.