Dec
28
2011

Log in to WordPress using an Email Address

by   |  Posted in Tutorials  |  13 comments

I needed to figure out how to allow a user to use either their username or email address to log into WordPress. It took a bit of rooting around but the final piece of code ended up being pretty simple.

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');

Place that in your functions.php file and you will be able to sign into your WordPress site using an email address or by default, the username.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , ,

Short URL: http://bit.ly/v4FKyV

Discussion 13 Comments

  1. slowegg on December 28, 2011 at 9:50 pm

    worked great…thanks!

  2. Pothi Kalimuthu on December 29, 2011 at 1:40 am

    Works fine here too. Thanks for the tip. Wonder why it can’t be made into the WordPress core. It is easy to remember the email than the username.

  3. kostenlos spielen on January 3, 2012 at 8:16 am

    Works great. Thanks for the effort

  4. Monsieur Z on January 3, 2012 at 7:59 pm

    Hey Bavotasan, debug says get_user_by_email is deprecated since 3.3, in favor of get_user_by(‘email’). Really great tip nonetheless!

    • c.bavota on January 4, 2012 at 12:46 pm

      Thanks for pointing that out.

  5. mfalme on January 5, 2012 at 1:57 pm

    I have been looking for a good login security solution.I think this together with pie register might be the perfect fix.thanks for making it look so easy.

  6. Jayshankar Krish on January 8, 2012 at 12:34 pm

    works like magic, wonderful i say!!! I know where to go incase i run into some trouble. Thanks again

  7. Massagesessel on January 11, 2012 at 6:55 am

    I was seraching for that so long. Finally i can integrade this on my site. Thanks a lot

  8. Brunello cucinelli on January 12, 2012 at 7:29 am

    Thanks this function saves us a lot of time for our blog management. Much appreciated.

  9. Nino Blasco on January 13, 2012 at 11:33 am

    Thanks for sharing it’s really useful.

  10. Giulia on January 17, 2012 at 11:59 am

    useful trick, thanks!

  11. Web Design Bournemouth on February 6, 2012 at 6:43 pm

    Thats awesome, I hate when sites don’t id or email. Because you always remember your email address but ID’s are often taken so your forced to use something else.

    GJ.

  12. junnydc on February 17, 2012 at 9:39 pm

    works great on my site with a child theme – classi. thanks

Leave a Reply

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

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.