So a few days ago i was asked how can I allow users to log in with email as well as user name, which seems to be more and more common on membership sites, so here is a quick tip.
Code Snippet
A simple copy paste in your theme's functions.php file would work but you can also create a custom plugin for that if you plan on changing themes in the future:
add_filter('authenticate', 'bainternet_allow_email_login', 20, 3); /** * bainternet_allow_email_login 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 */ function bainternet_allow_email_login( $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 ); }
Breakdown
First line is where we hook our `bainternet_allow_email_login` function in to `authenticate` filter hook.
next on line number 3 we check if the user has entered an email instead of a user name and if he did (moving on to lines 4-5) we get the user name of a user with the entered email (if exists).
Bonus
Well now that we can log in using email address we just need to let the user know about it and change the `Username` label to something like `Username Or Email` like the images bellow:
So we use the `gettext` filter hook like this:
add_filter( 'gettext', 'addEmailToLogin', 20, 3 ); /** * addEmailToLogin function to add email address to the username label * @param string $translated_text translated text * @param string $text original text * @param string $domain text domain */ function addEmailToLogin( $translated_text, $text, $domain ) { if ( "Username" == $translated_text ) $translated_text .= __( ' Or Email'); return $translated_text; }
Wrapping it up
Here is the full code as a very lightwieght plugin:
<?php /* Plugin Name: Allow Email Login Plugin URI: en.bainternet.info Description: Username Allow Email LogIn Version: 1.0 Author: Bainternet Author URI: en.bainternet.info */ add_filter('authenticate', 'bainternet_allow_email_login', 20, 3); /** * bainternet_allow_email_login 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 */ function bainternet_allow_email_login( $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 ); } add_filter( 'gettext', 'addEmailToLogin', 20, 3 ); /** * addEmailToLogin function to add email address to the username label * @param string $translated_text translated text * @param string $text original text * @param string $domain text domain */ function addEmailToLogin( $translated_text, $text, $domain ) { if ( "Username" == $translated_text ) $translated_text .= __( ' Or Email'); return $translated_text; }
Simple and clean.