Last active
May 21, 2016 02:14
-
-
Save kjbrum/9206cf3339c106a21713 to your computer and use it in GitHub Desktop.
Allow users to login to the site with their username or email.
This file contains hidden or 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 | |
/** | |
* Allow users to login to the site with their username or email. | |
* | |
* @param object $user WP_User object | |
* @param string $username_email The username or email entered for login | |
* @param string $password The users password | |
* @return object The authenticated WP_User object | |
*/ | |
function wp_allow_user_login_with_email( $user, $username_email, $password ) { | |
// Check for empty fields | |
if( empty( $username_email ) || empty( $password ) ) { | |
$error = new WP_Error(); | |
if( empty( $username_email ) ) | |
$error->add( 'empty_username', __( '<strong>ERROR</strong>: The username/email field is empty.' ) ); | |
if( empty( $password ) ) | |
$error->add( 'empty_password', __( '<strong>ERROR</strong>: The password field is empty.' ) ); | |
return $error; | |
} | |
// Check if user exists in WordPress database | |
if ( ! $user = get_user_by( 'login', $username_email ) ) { | |
if( ! $user = get_user_by( 'email', $username_email ) ) { | |
return new WP_Error( 'invalid_username', sprintf( __( '<strong>ERROR</strong>: Invalid username/email. <a href="%s">Lost your password?</a>' ), wp_lostpassword_url() ) ); | |
} | |
} | |
// Filter whether the given user can be authenticated with the provided $password. | |
$user = apply_filters( 'wp_authenticate_user', $user, $password ); | |
if ( is_wp_error( $user ) ) | |
return $user; | |
// Check if the password used is valid for the user | |
if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) | |
return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username/email <strong>%1$s</strong> is incorrect. <a href="%2$s">Lost your password?</a>' ), $username_email, wp_lostpassword_url() ) ); | |
return $user; | |
} | |
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20 ); | |
add_filter( 'authenticate', 'allow_user_login_with_email', 20, 3 ); | |
/** | |
* Modify the label of the username field on the login page | |
* | |
* @param string $translated_text The new text | |
* @param string $text The current text | |
* @param string $domain The sites domain name | |
* @return string The new text | |
*/ | |
function modify_username_label( $translated_text, $text, $domain ) { | |
if ($text === 'Username') { | |
$translated_text = 'Username/Email'; | |
} | |
return $translated_text; | |
} | |
add_action( 'login_head', function() { | |
add_filter( 'gettext', 'modify_username_label', 20, 3 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment