Last active
January 31, 2017 21:16
-
-
Save petersplugins/5a6cf7e3a55a74aef7756e94d362bcba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This code snippet forces login with email address and eliminates login with user name | |
// Remove the default authentication function and replace it by a custom function | |
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 ); | |
add_filter( 'authenticate', 'force_email_login', 20, 3 ); | |
// Custom function to force email login | |
function force_email_login( $user, $username, $password ) { | |
if ( ! empty( $username ) ) { | |
// Is there a user with the given email address? | |
$user = get_user_by( 'email', str_replace( '&', '&', $username ) ); | |
if ( ! $user ) { | |
// No user with the given mailaddress was found, so we set the username to a silly string, that will always result | |
// in an invalid username error - so we don't have to write our own code and we don't give any hints, because | |
// the default WordPress error message will be shown | |
$username = 'THIS_IS_NOT_A_VALID_USERNAME_AND_LEADS_TO_AN_ERROR_WHEN_TRYING_TO_LOG_IN_WITH_THIS_SILLY_STRING'; | |
} else { | |
// We found an user with the given mailaddress, so we set the username to the user's name | |
$username = $user->user_login; | |
} | |
} | |
// Now we call the default authentication function and let WordPress do the rest | |
return wp_authenticate_username_password( null, $username, $password ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment