Snippet: Login to WordPress with an Email Address

Shortly after launching our store, powered by WooCommerce, we received feedback about how annoying it was to have to remember a username to login to our site. So I went searching for a solution. My first stop included the WP E-mail Login plugin. It’s a great plugin but I’m not a fan of plugins unless their necessary.

So then I came across the handy little snippet below that provides the same solution as the WP E-mail Plugin. Throw this snippet into your functions.php file and you’re all set. If all goes well you’ll notice your login box change from “Username” to “Username / Email”.

Tip: Couple this snippet with the SimpleModal Login plugin and you’re all set!

// Allow users to login with their e-mail address
function login_with_email_address($username) {
        $user = get_user_by('email',$username);
        if(!empty($user->user_login))
                $username = $user->user_login;
        return $username;
}
add_action('wp_authenticate','login_with_email_address');
function change_username_wps_text($text){
       if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
         if ($text == 'Username'){$text = 'Username / Email';}
            }
                return $text;
         }
add_filter( 'gettext', 'change_username_wps_text' );

If you enjoyed this post shoot me back a link or even better donate $10 to the
Navy SEAL Foundation. Cheers!