How to Allow User to Log In With Email in WordPress


By default, WordPress allows users to log in with their user name. However, if you wish to provide the feature to allow the users to login using their email addresses, you could easily do that by adding a tiny amount of code to your functions.php file.
All you need to do is copy the following code and paste it inside your functions.php file located inside the root of your theme folder.
  1. // Allow users to login with email address  
  2. function login_with_email_address( &$username, &$password ) {  
  3.     $user = get_user_by( 'email'$username );  
  4.     if( !emptyempty$user->user_login ) ) {  
  5.         $username = $user->user_login;  
  6.     }  
  7. }  
  8. add_action( 'wp_authenticate''login_with_email_address' );  
We'll be using the wp_authenticate action to perform this operation, since it gets triggered right before the WordPress authentication system tries to log in the user.