Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Username only lowercase and numbers in WP multi?

We are using wordpress in multi sub-domains at work. So we have administrator for each domain who can create users for subscribing editing contributing. The problem is, our company edit the rule that all logins/usernames have to be like this "first-name.family-name" ex "barack.obama"... :-) But the administrator can not do it, only super admin can do it ! In sub-domain administrator mode, there is a message that says : "Only the lowercase letters a-z and numbers allowed".

So we want to make it possible to admin to create users with first-name.family-name, but we try to search how to change it in the code, without finding it.

If someone can help, thanks !

like image 842
Jaswinder Kaur Avatar asked Aug 31 '25 01:08

Jaswinder Kaur


1 Answers

In multisite usernames are validated with wpmu_validate_user_signup, which you can find in /wp-includes/ms-functions.php#L462.

You can edit that file, add a period after the 0-9 in the preg_match regex. However, this is not a recommended approach because it will get overwritten each time WordPress core is updated to newer version.

if ( $user_name != $orig_username || preg_match( '/[^a-z0-9.]/', $user_name ) ) {

A better solution would be to create a plugin for it.

You could try to add the following in a php-file and add it to /wp-content/mu-plugins/

<?php
/*
Plugin Name: wpmu no username error
*/

function wpmu_no_username_error( $result ) {
    $error_name = $result[ 'errors' ]->get_error_messages( 'user_name' );
    if ( empty ( $error_name ) 
        or false===$key=array_search( __( 'Only lowercase letters (a-z) and numbers are allowed.' ), $error_name)
    ) {
    return $result;
    }
//  only remove the error we are disabling, leaving all others
    unset ( $result[ 'errors' ]->errors[ 'user_name' ][$key] );
/**
 *  re-sequence errors in case a non sequential array matters
 *  e.g. if a core change put this message in element 0 then get_error_message() would not behave as expected)
 */
    $result[ 'errors' ]->errors[ 'user_name' ] = array_values( $result[ 'errors' ]->errors[ 'user_name' ] );
    return $result;
}
add_filter( 'wpmu_validate_user_signup', 'wpmu_no_username_error' );

I didn't try it though. This is based on "Changing the username character limit from four to less characters".

I hope this helps. At least it should get you close. GL with it!

like image 156
Axel Avatar answered Sep 02 '25 23:09

Axel