Amend error msg on incorrect password with email login enabled
-
Hi
My site is set to Email only for login, and recently one of my users got very confused because when she put in an incorrect password, the error message told her “The password you entered for the username [username] is incorrect”. The username was of course similar to her email but not the same, so she started trying to log in with her username instead.
I have put the snippet into my functions file so that the email address is displayed in the error message instead of the username. I have also tested using it directly in the plugin login.php file instead and I think it works well. Would you consider putting it in login.php for the next version please?
Thanks!
Jon// when email login is enabled we need to change the error message for incorrect password add_filter( 'authenticate', 'wppb_change_incorrect_password_error_with_email', 50, 3 ); //NB filter priority runs runs after password has been checked function wppb_change_incorrect_password_error_with_email($user, $username, $password) { //check whether login has failed if (is_wp_error($user)) { //if reason for failure is incorrect password if ( $error_msgs = $user->get_error_messages('incorrect_password') ) { $wppb_generalSettings = get_option( 'wppb_general_settings' ); // if email login is enabled, replace user login with user email in error message if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ){ $logging_in_user = get_user_by( 'login', $username ); //cycle through error messages replacing username with email foreach ($error_msgs as $key => $error_msg) { $error_msgs[$key] = str_replace( $username, $logging_in_user->user_email, $error_msg ); } //update $user (which is a WP_error object) $error_data = $user->get_error_data('incorrect_password'); //get existing error data if there is any $user->remove('incorrect_password'); //remove current error messages foreach ($error_msgs as $key => $error_msg) { //add back updated error messages $user->add('incorrect_password', $error_msg, $error_data); } } } } return $user; }
- The topic ‘Amend error msg on incorrect password with email login enabled’ is closed to new replies.