Support » Fixing WordPress » Email as Username in registration

  • Resolved ste_yeu

    (@ste_yeu)


    Hello,

    many users would to allow the registration in a wp site without an username, just only with an email.

    This is a core problem, so the solution (a trick) is to replace username with email.

    If wp requires an username and an email for the registration, we can get email value and put it in username value. In the registration form users will see two fields:

    1. your email
    2. repeat your email

    This is a trick, because for wp the real fields will be:
    1. username (as your email)
    2. your email (as repeat your email)

    How can we do this?

    Should we insert a code in functions.php file?.. something like this:

    add_action( 'wp_core_validate_user_signup', 'custom_validate_user_signup' );
    
    function custom_validate_user_signup($result)
    {
      unset($result['errors']->errors['user_name']);
    
      if(!empty($result['user_email']) && empty($result['errors']->errors['user_email']))
      {
        $result['user_name'] = md5($result['user_email']);
        $_POST['signup_username'] = $result['user_name'];
      }
    
      return $result;
    }

    Thank you in advance!

    • This topic was modified 2 years, 5 months ago by  ste_yeu.
Viewing 6 replies - 1 through 6 (of 6 total)
  • You are correct that wordpress core does require username.

    I would suggest not to edit that rule.

    Simply, remove the username textfield and the username error.

    You can use something like this in your functions.php:

    add_action('login_form_register', function(){
        if(isset($_POST['user_login']) && isset($_POST['user_email']) && !empty($_POST['user_email'])){
            $_POST['user_login'] = $_POST['user_email'];
        }
    });

    You do have an option to use plugin like this: https://wordpress.org/plugins/smart-wp-login/

    Thank you a2hostinglk!

    At the moment I use WP User Manager plugin, that allows to change the label of the fields, so I replaced “Username” with “Insert Your Email” and “Email” with “Repeat Your Email”.

    But it’s not the best solution, I haven’t a control if the values​entered in the fields match, but it works…

    However I’ll try your code đŸ˜‰

    You’re welcome. I hope it helps!

    @a2hostinglk

    Your code works fine, thank you.

    @ste_yeu

    You’re welcome đŸ™‚

    I am glad I was able to help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Email as Username in registration’ is closed to new replies.