3

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)

But there is another problem: is the @ (at) an allowed character for username?

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!

  • since WordPress 4.6 it's possible to log in with e-mail and password. then there is no more trick needed – mmm Oct 7 '16 at 10:19
  • @mmm I know this, but I need to synchronize Username name value with Email value (the only important to require) to better handle errors, for example if an user enters two emails that do not match, etc... I need a cleaner work. ;) – ste_yeu Oct 7 '16 at 14:47
3

You can use @ and . in usernames, so there is no problem. Now you could create your own register form and use register_new_user(). You could even manipulate wp_registration_url() with the filter register_url, so the register link would point to your new register page.

If you want to use it with the standard interface provided by wp-login.php, you would need some workarounds. Lets say, you only want to display the email input field for registration, you could simply (well, its a bit hacky though) hide the username field with something like this:

add_action( 'register_form', function() {
    ?><style>#registerform > p:first-child{display:none;}</style><?php
} );

Sidenote: There is also a possibility to enqueue stylesheets into the wp-login.php using the login_enqueue_scripts action.

But if you do so you will send an empty username, so you have to hook into two filters: sanitize_user and validate_username.

add_filter( 'sanitize_user', function( $sanitized_user, $raw_user, $strict ) {
    if ( $raw_user != '' ) {
        return $sanitized_user;
    }

    if ( ! empty ( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' && is_email( $_POST['user_email'] ) ) {
        return $_POST['user_email'];
    }

    return $sanitized_user;
}, 10, 3 );

add_filter( 'validate_username', function( $valid, $username ) {
    if ( $valid ) {
        return $valid;
    }

    if ( ! empty ( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' && is_email( $_POST['user_email'] ) ) {
        return true;
    }

    return is_email( $username );
}, 10, 2 );

In sanitize_user we replace the empty string with the email address. Later the username will be validated with validate_username, but again, the empty username will be used, so we need to catch this too.

But I think to create an own form would be preferred and less hacky.

  • 1
    You are on the right track, but you cannot return $_POST['user_email'] as username, that is not sanitized and will result in having invalid characters in the WP username; and will cause problems down the line. – user42826 Oct 7 '16 at 13:57
  • Yeah, Not 100% sure myself, but can't find an example where something in $_POST['user_email'] would pass as a valid email and still cause problems as a username. – websupporter Oct 7 '16 at 14:42
  • So, isn't there a function that gets "email value" and inserts it in "username value"??? :/ – ste_yeu Oct 7 '16 at 14:59
  • You could use the action user_register to alter the username after successful registration again to make sure. codex.wordpress.org/Plugin_API/Action_Reference/user_register But haven't seen some other sanitisation on user_email. Will check again. – websupporter Oct 7 '16 at 16:13
  • 1
    This code works fine for wp core, just put it in the functions.php file: 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']; } }); – ste_yeu Oct 8 '16 at 8:20
2

Here's what I used:

jQuery(document).ready(function($){
    // hide the username field.
    $('input#user_login').parent().parent().hide();
    // force username to mirror the input of the email field
    $('input#email').on('change',function(){
        $('#user_login').val($(this).val());
    });
});

Nice and simple.

1

Add this in functions.php (or other php file) :

add_action('login_footer', function() {
?><script type="text/javascript">
    document.querySelector('input#user_login').parentElement.hidden = true ;
    document.querySelector('input#user_email').onchange = function(){
        document.querySelector('input#user_login').value = this.value ;
    } ;
</script><?php
} );

The action will print the script in the wp-login.php "footer". The javascript hide the "user_login" label and input, and set its value with the e-mail input value.

  • Please edit your answer, and add an explanation: why could that solve the problem? – fuxia Feb 4 at 13:07

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.