I need to change this error message from including the username to the users email address.
I’ve checked this question and answer out and it helped, but didn’t go far enough for me.
Change login error messages
I took this code from the wp_includes/user.php
core file and I know that I can use the filter login_errors
to override this message. My question is what variables do I have access to in this filter? How do I switch this out for the email?
'<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?'
Here’s what I have so far, but I don’t know what variable I need to use to get it working.
add_filter('login_errors','cc_login_error_message');
function cc_login_error_message($error){
//check if that's the error you are looking for
$pos = strpos($error, 'incorrect');
if (is_int($pos)) {
//its the right error so you can overwrite it
$error = sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ), $user_email, wp_lostpassword_url() );
}
return $error;
}
Any ideas?
Specifically the $user_email variable in my script returns nothing, I’m not sure if there is such a variable, but that one I just made up.
There may be an easier way to do this, but you could rewrite the
wp_authenticate_username_password()
function in thewp-includesclass-wp-error.php
file.Of course you cannot edit the function directly, but since this function is a filter function, you can remove the filter, copy it, add the copied function back in and test it. After testing that it still produces the same error, you can then change the error in the copied code.
The following code removes the old WordPress function and creates a new function with a unique name.
The next step is to test the code to make sure it is producing the same error as before. Once tested, try changing the error message as you wish. If things go too wrong, start over or comment out the first 2 lines and the WordPress function will use the old error message.
You can use REQUEST variables to know the username used to login, then check if user exist to get his email (which not recommended, as someone may try to get email addresses of users):
it shows: