How to Let Users Login to WordPress Using an Email Address

Tutorials

How many times have you forgotten the username to your site? In today's Weekend WordPress Project, find out how to easily enable email login.

banner image

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.

All the good WordPress stuff, once every two weeks

Subscribe

Leave a comment