It’s possible to add a custom error message on the WordPress login page. To customize the WordPress login error messages, you can use the login_errors
filter. This filter allows you to modify or replace the default error messages displayed on the login page. Here’s an example of how you can customize the login error messages:
function wpsnippets_customize_login_errors( $error ) {
// Customize the error messages here
$custom_error_messages = array(
'empty_username' => __( 'Please enter your username.' ),
'empty_password' => __( 'Please enter your password.' ),
'invalid_username' => __( 'Invalid username. Please try again.' ),
'invalid_password' => __( 'Invalid password. Please try again.' ),
);
// Replace default error messages with custom error messages
$error = array_intersect_key( $custom_error_messages, $error );
// Return the modified error messages
return $error;
}
add_filter( 'login_errors', 'wpsnippets_customize_login_errors' );
In this example, we define an associative array $custom_error_messages
where the keys represent the specific error codes, and the values represent the custom error messages you want to display. You can customize these messages according to your needs.
The login_errors
filter receives the default error messages as an array. We use array_intersect_key()
to replace the default error messages with the custom error messages defined in $custom_error_messages
. Any error codes not present in the custom error messages array will not be modified.
With this code snippet, you can create a custom error message on the WordPress login page to provide more specific instructions or feedback to your users.