How to Let Users Login to WordPress Using an Email Address

How to Let Users Login to WordPress Using an Email Address

It’s not unusual for users to forget the username they chose when signing up for a WordPress site.

Or, when registering for a site, a user might discover the username they want is already taken.

Fortunately, you can give users the option to login to your site with their email address, which they are less likely to forget.

In today’s Weekend WordPress Project, I’ll show you how to use a code snippet to easily add email login to your site.

Adding Email Login with Code

The first thing we need to do is remove the default authentication rights. Add the following snippet to your functions.php file:

Next, we’re going to add our own authentication. To do so, we’re going to use add_filter.

Add the following code to your functions.php files:

Here’s how it works:

The code checks if the username (now email) or password fields are empty. If neither are empty, it uses get_user_by to look for the user’s email. After finding a valid user, it then checks if the password is correct using the wp_check_password() function.

Thanks to Nishant Kumar at The Binary for this snippet.

Do users on your site forget their passwords? What other kinds of Weekend WordPress Projects would you like us to feature? Let us know in the comments below.

Tags:
Raelene Morey

Raelene Morey Raelene is the Founder of Words By Birds, a digital writing agency that works with startups, SaaS, B2B, and WordPress businesses on turning tech speak into words that convert. She was formerly the managing editor at WPMU DEV. A computer science grad turned newspaper journalist, when she’s not taming browser tabs, she likes brunching and bushwalking.