Username Rules for New Registrations
Requires new usernames to be a sensible length, use only letters, numbers, dots, dashes and underscores, and not be just a number.
Opens WPIDE on
Code
$wpide_username_min = 4;
$wpide_username_max = 30;
add_filter( 'registration_errors', function ( $errors, $login ) use ( $wpide_username_min, $wpide_username_max ) {
$length = strlen( $login );
if ( $length < $wpide_username_min || $length > $wpide_username_max ) {
$errors->add( 'wpide_username_length', sprintf( '<strong>Error:</strong> Usernames must be between %d and %d characters.', $wpide_username_min, $wpide_username_max ) );
}
if ( ! preg_match( '/^[A-Za-z0-9._-]+$/', $login ) ) {
$errors->add( 'wpide_username_chars', '<strong>Error:</strong> Usernames may only use letters, numbers, dots, dashes and underscores.' );
} elseif ( ctype_digit( $login ) ) {
$errors->add( 'wpide_username_digits', '<strong>Error:</strong> Usernames cannot be only numbers.' );
}
return $errors;
}, 10, 2 );
Notes
Existing usernames are not affected. Adjust the length limits at the top.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.