Email as Username in registration

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.

Leave a Comment