How to translate “ERROR: Invalid username. Lost your password?”

In the en_GB version of the .mo translation file, this string is available:

<strong>ERROR</strong>: Invalid username. <a href=\"%s\" title=\"Password Lost and Found\">Lost your password</a>?

and the reference is from wp-includes/user.php on line 93. I found this using Poedit and searching using Ctrl + F.

From line 93 we get the following code:

return new WP_Error( 'invalid_username', sprintf( __( '<strong>ERROR</strong>: Invalid username. <a href="https://wordpress.stackexchange.com/questions/127702/%s" title="Password Lost and Found">Lost your password</a>?' ), wp_lostpassword_url() ) );

Which means you should write a function for your functions.php hooking into invalid_username like so:

add_filter( 'wp_login_errors', 'override_incorrect_username_message', 10, 2 );
function override_incorrect_username_message( $errors, $redirect_to ) {
    if( isset( $errors->errors['invalid_username'] ) ) {
        $errors->errors['invalid_username'][0] = 'Your new translated message';
    }

    return $errors;
 }

You can hunt around in wp-includes.php for other similar error messages which you can fix in a similar way.

Leave a Comment