Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


User login via E-mail in WordPress

Stanislav KhromovStanislav Khromov

Update 2017-04-03: This feature is included in core with WordPress versions 4.5 or later.

Here is a quick snippet to allow users on your site to sign in via their e-mail as well as their password.

Save it as a file in your plugins/ folder and activate it normally as a plugin.

<?php
/*
Plugin Name: Login with E-mail
Plugin URI: https://snippets.khromov.se/user-login-via-e-mail-in-wordpress/
Description: Lets users log in via their E-mail as well as their username
Version: 2014.07.03
Author: khromov
Author URI: https://snippets.khromov.se
License: GPL2
*/

add_filter('authenticate', function($user, $username, $password)
{
    if (is_email($username))
    {
        $user = get_user_by('email', $username);
        if($user)
            $username = $user->user_login;
    }
    return wp_authenticate_username_password(null, $username, $password);
}, 20, 3);

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 5
  • Susan
    Posted on

    Susan Susan

    Reply Author

    Why make things so complicated? As for these aspects, the utilization of WordPress plugin is my first priority. Here, I highly recommend WP Email Login. After activation, you can just log out your dashboard and try to log in with your email address. I have shared the detailed steps at https://hostingreview360.com/how-to-allow-users-to-login-with-email-wordpress/. You can have a look as the reference.


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Hey Susan,

      The plugin you linked would work fine.

      Making your own plugin serves two purpose – understanding the inner workings and keeping track of the code that is running on your WP site. This isn’t always required but can be a worthwile exercise.


      • Susan
        Posted on

        Susan Susan

        Reply Author

        Yes, you are absolutely right,


  • Collins Agbonghama
    Posted on

    Collins Agbonghama Collins Agbonghama

    Reply Author

    WordPress core now support login via email. Not sure if this code is still needed.


    • Stanislav Khromov
      Posted on

      Stanislav Khromov Stanislav Khromov

      Reply Author

      Good catch Collins! Added a notice at the top of the post, thanks.