Remove Username Character Limit from WordPress Multi-Site / Multi-User

By Lew Ayotte  |  May 13th, 2010  |  Published in WordPress, WordPress Plugins  |  10 Comments

I’ve been working on a pretty complex project with WordPress MultiUser (soon to be MultiSite). This client needs several sites with hundreds of users divided into each site. I will be integrating the backend authentication with LDAP and discovered that a small percentage of their users have usernames with fewer than four characters.

WordPress MU currently has a minimum limit of four characters set in its core. Unfortunately, this limit is still imposed in WordPress MS 3.0. The limit is probably there because usernames were used for the domain too and WP-Devs didn’t want to conflict with country codes. But that is not an issue for my client, so I wanted to kill the limit (without touching core).

Basically, I wrote a quick mu-plugin that unset the error message when someone tries to add a user with fewer than four characters. Doing this removes any halts that would stop processing the new user. Here is my code:

function remove_username_char_limit($result) {
  if ( is_wp_error( $result[ 'errors' ] ) && !empty( $result[ 'errors' ]->errors ) ) {

    // Get all the error messages from $result
    $messages = $result['errors']->get_error_messages();
    $i = 0;
    foreach ( $messages as $message ) {

      // Check if any message is the char limit message
      if ( 0 == strcasecmp("Username must be at least 4 characters", $message)) {
        // Unset whole 'user_name' error array if only 1 message exists
        // and that message is the char limit error
        if ( 1 == count($messages) ) {
          unset( $result['errors']->errors['user_name'] );
        } else {
          // Otherwise just unset the char limit message
          unset( $result['errors']->errors['user_name'][$i] );
        }
      }	

      $i++;
    }
  }

  return $result;
}
add_action('wpmu_validate_user_signup', 'remove_username_char_limit');

Responses

  1. Andy says:

    May 31st, 2010 at 3:07 pm (#)

    Hey Lew

    I tried this out and got the following error:

    Warning: Cannot modify header information – headers already sent by (output started at /home/band/public_html/wp-content/themes/twentyten/functions.php:588) in /home/band/public_html/wp-includes/pluggable.php on line 890

    Any ideas?

    Thanks in advance

    Andy

  2. Andy says:

    May 31st, 2010 at 3:19 pm (#)

    My bad your function worked perfectly…I had some empty characters at the end of my functions.php that was causing the problem

    Thanks heaps!

  3. Lew Ayotte says:

    May 31st, 2010 at 8:40 pm (#)

    Great! Glad everything is working :)

    Lew

  4. Lee says:

    June 21st, 2010 at 7:48 pm (#)

    You are a lifesaver. As you see my name "Lee" wouldn't be accepted. What is user "Ed" going to do? :)

    Clever workaround and my core is still intact. Thanks so much!

  5. Lars Koudal says:

    August 18th, 2010 at 3:49 am (#)

    Very nicely done. Now, do you happen to have a solution for removing the text "(Must be at least 4 characters, letters and numbers only.)" displayed as an instruction underneath the username input field?

    If necessary I could go create a custom translation file, where I leave that field empty, but that is a pretty ugly hack in my opinion, and complicated.

    In any case, thanks for this tip, I was in the exact same situation with a company that has usernames with down to 2 characters!

  6. Jeff says:

    August 27th, 2010 at 4:16 pm (#)

    Any chance you could edit this to make it work in the backend under the add-user field? I hate to hack my core, but not sure how to write a plugin to do this…

    Thanks for your work on this great (and needed) plugin!

  7. Lew Ayotte says:

    August 27th, 2010 at 9:19 pm (#)

    Hi Jeff,

    Not sure I know what you’re asking for. This is a mu-plugin, if you stick this PHP in a file in the wp-content/mu-plugins/ folder it doesn’t require you to edit core.

    Lew

  8. Lew Ayotte says:

    August 27th, 2010 at 9:29 pm (#)

    Lars,

    You could create your own “translation” of the text… http://codex.wordpress.org/Translating_WordPress

    Lew

  9. Silvan says:

    March 21st, 2011 at 12:16 pm (#)

    If you use:

    if ( 0 == strcasecmp(__(“Username must be at least 4 characters”, $message))

    it will be safe for translations too.

  10. Lew Ayotte says:

    March 21st, 2011 at 1:32 pm (#)

    OH yeah, good point Silvan, I didn’t even think about that. Thanks.

Leave a Response