2

I am using Wordpress 4.9.4 in a Wordpress Multisite, and I need to allow users to register using their email address as username. Looking at the code I found that what prevents me from accomplish that is that in the ms-functions.php there is a regular expression filtering usernames that have other characters than a-z, so when an email is the username I would get "Usernames can only contain lowercase letters (a-z) and numbers." I changed that regular expression in the ms-functions.php and it works fine. Now my question is, I don't want to have to recode the function wpmu_validate_user_signup every time I update my wordpress. What would be good practices regarding this issue? Should I create another includes/ms-functions.php in a theme_child? If so, can I create only this function or I have to copy the entire ms-functions.php?

Thanks

  • 3
    WordPress users can always log in with their username OR email. emails can be changed anytime, us names cannot change. Theres no need to tell users their username just tell them to login with their email.. also was pretty sure i made users with identical usernames/ emails before but maybe im wrong. if you changed the core WordPress code then that is never the right way to go about changing something. – Joel M Feb 24 '18 at 6:09
  • 1
    In Wordpress multi-site there is a filter for username, so usernames cannot contain @ or any other character. (Although it seems that single installations of WP do allow email addresses as usernames) – Gonzalo Feb 24 '18 at 6:20
2

Emails can not be used as user names as wordpress uses user names by default as part of urls (for example the author page). Therefor it is not enough to let users register with email addresses, you also need to have some code to handler the URL related issues.

Anyway this sounds like an XY problem. Since users can login with their email address why would any one actually care what is their login handle created at registration time? The most simple thing to do is to create a registration form in which you create a "sanitized" user name for the user based on his email if you do not want him to think of one.

  • Thanks @Mark Kaplun for the answer. As you said, I would care about the registration username but my client required. The user is not allow to blog or post commentaries so, the URL shouldn't be a problem. I have my own registration page for users using Gravity Forms, but the issue is that Gravity Forms makes a call to WP's function to register the username, and I keep getting the problem of having an @ in the username. – Gonzalo Feb 24 '18 at 7:01
  • Than the easiest thing might be to filter the form handling instead of the core functionality, as filtering the core functionality might impact other users that can become authors – Mark Kaplun Feb 24 '18 at 7:05
  • ... or trigger the core filtering only the form is processed, in which case you might want to look at the sanitize_user filter – Mark Kaplun Feb 24 '18 at 7:08
  • This answer's giving some misinformation. You CAN use an email address to register (after removing the character restriction validation). WordPress doesn't use your username for author URLs, it uses the "nicename" which is sanitized. However, if you use an email as your username it's worth noting your user_login will strip the @ symbol and potentially other characters. It still does work, and you can log in using your email address now. I don't know of any downsides. – Radley Sustaire Oct 24 '18 at 13:32
  • while your emphasize on nicename, is technically correct, I do not see how your comment shows any problem with the answer. everything you wrote is actually written in the first paragraph ;) – Mark Kaplun Oct 24 '18 at 14:34
1

I found another solution in a website. By adding this code to the top of the class of the registration plugin (in this case Gravity Forms) overrides the validation and allows the email to be used as username:

function custom_register_with_email($result) {

   if ( $result['user_name'] != '' && is_email( $result['user_name'] ) ) {

      $result['errors']->remove('user_name');

   }

   return $result;
}
add_filter('wpmu_validate_user_signup','custom_register_with_email');
  • Be careful, this removes a lot of unrelated validation steps as well. Including one that restricts "illegal names", the minimum 4 character limit, the maximum 60 character limit, and others. – Radley Sustaire Oct 24 '18 at 13:33
1

Here is a safer version of Gonzalo's filter, which only removes the validation error that reads "Usernames can only contain lowercase letters (a-z) and numbers.". It instead uses a similar validation but allows the same characters seen in sanitize_email().

The only caveat is this checks the error message name. If WordPress changes that error message in the future, this will stop working. But it's the only way to target that specific validation message.

/**
 * Allow users to sign up using an email address as their username. 
 * Removes the default restriction of [a-z0-9] and replaces it with [a-z0-9+_.@-].
 *
 * @param $result
 *
 * @return array $result
 */
function wpse_295037_disable_username_character_type_restriction( $result ) {
    $errors = $result['errors'];
    $user_name = $result['user_name'];

    // The error message to look for. This should exactly match the error message from ms-functions.php -> wpmu_validate_user_signup().
    $error_message = __( 'Usernames can only contain lowercase letters (a-z) and numbers.' );

    // Look through the errors for the above message.
    if ( !empty($errors->errors['user_name']) ) foreach( $errors->errors['user_name'] as $i => $message ) {

        // Check if it's the right error message.
        if ( $message === $error_message ) {

            // Remove the error message.
            unset($errors->errors['user_name'][$i]);

            // Validate using different allowed characters based on sanitize_email().
            $pattern = "/[^a-z0-9+_.@-]/i";
            if ( preg_match( $pattern, $user_name ) ) {
                $errors->add( 'user_name', __( 'Username is invalid. Usernames can only contain: lowercase letters, numbers, and these symbols: <code>+ _ . @ -</code>.' ) );
            }

            // If there are no errors remaining, remove the error code
            if ( empty($errors->errors['user_name']) ) {
                unset($errors->errors['user_name']);
            }
        }
    }

    return $result;
}
add_filter( 'wpmu_validate_user_signup', 'wpse_295037_disable_username_character_type_restriction', 20 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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