WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get allow only lowercase usernames are valid usernames in my wordpress blog. I managed to write a function but it does not seem to work.

add_filter('validate_username' , 'simple_user', 1, 2);
    function simple_user($valid, $username ) {
            if (preg_match("/[a-z0-9]+/", $username)) {
                // there are spaces
                return $valid=false;
        return $valid;

Any ideas with getting this to work ? i tried this but it never worked. I will be very appreciative if someone could help me with this one. Thank you

share|improve this question
why not just use strtolower($username); ? – Jamie Jan 29 '13 at 23:12
up vote 1 down vote accepted

The filter validate_username sends and expects a boolean value, not a string.

Hook into sanitize_user and use mb_strtolower().

Sample code, not tested:

add_filter( 'sanitize_user', 'wpse_83689_lower_case_user_name' );

function wpse_83689_lower_case_user_name( $name )
    // might be turned off
    if ( function_exists( 'mb_strtolower' ) )
        return mb_strtolower( $name );

    return strtolower( $name );
share|improve this answer
Thank you, Works perfectly :-) – GeYan Zmith Jan 30 '13 at 6:12
+1 for using mb_strtolower if available and falling back on strtolower if not. – akTed Jan 30 '13 at 9:00

Just found this on wordpress' codex site for the sanitize_user filter.

add_filter('sanitize_user', 'strtolower')

According to the site this will force usernames to be lowercase.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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