lafif-a
3/5/2015 - 7:49 AM

[Wordpress] Allow user login with email

[Wordpress] Allow user login with email

<?php

/**
 * login_with_email filter to the authenticate filter hook, to fetch a username based on entered email
 * @param  obj $user
 * @param  string $username [description]
 * @param  string $password [description]
 * @return boolean
 */
add_filter('authenticate', 'login_with_email', 20, 3);
function login_with_email( $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 );
}