Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.