Allow Users to Login With Email Address in WordPress

How to Allow Users to Login With Email Address in WordPress

in WordPress Admin
Google+ LinkedIn

WordPress by default assign a unique username to each user on registration. As usernames are unique on a WordPress website, there is a fine possibility that the username a user wants to register has already been taken by another user, in that case, he have to choose a different username that user might not remember later. And if they don’t remember their username then it will be very hard for them to login to your website. I am sure, you must have faced similar situation in past and know how frustrating this is.

The following snippet will help you fix this issue. Now, your users will be able to login with email address assigned to their account. We will use wp_authenticate action for this purpose since it runs before WordPress tries to authenticate the user details. We will change the default authentication method used by WordPress and force it to check for email address against user submitted username.

To allow users to login with email address, simply paste this code snippet in your theme’s functions.php file.

// Allow users to login with email address
function wpcs_login_email_address( &$username, &$password ) {
    $user = get_user_by( 'email', $username );
    if( !empty( $user->user_login ) ) {
        $username = $user->user_login;
    }
}
add_action( 'wp_authenticate', 'wpcs_login_email_address' );

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *