<?php
/**
 * 
 * Plugin Name: Log-in from Email or Username 
 * Plugin URI: http://test218.irontec.com
 * Description: Log-in from Email or Username
 * Version: 1.0
 * Author: Daniel
 * Author URI: http://test218.irontec.com
 */

/**
 * filtro de authenticate, para iniciar sesión con el email o el nombre de usuario.
 * @param string $user
 * @param string $username
 * @param string $password
 */

function it_email_login_authenticate( $user, $username, $password ) {
    
    if ( is_a( $user, 'WP_User' ) ) {
        
        return $user;
    }
    
    if ( !empty( $username ) ) {
        
        $username = str_replace( '&', '&amp;', stripslashes( $username ) );
        $user     = get_user_by( 'email', $username );
        
        if ( isset( $user, $user->user_login, $user->user_status ) && 0 == (int) $user->user_status ) {
            
            $username = $user->user_login;
        }
    }
    
    return wp_authenticate_username_password( null, $username, $password );
}

remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
add_filter( 'authenticate', 'it_email_login_authenticate', 20, 3 );
