To change the login error message in WordPress, you can use the login_errors
filter hook. This allows you to modify the error message displayed when a user enters incorrect login credentials.
Here’s an example code snippet that demonstrates how to change the login error message:
function wpsnippets_custom_login_error_message() {
return 'Your custom error message goes here.';
}
add_filter( 'login_errors', 'wpsnippets_custom_login_error_message' );
In the code above, we define a custom function wpsnippets_custom_login_error_message
that returns the desired error message. We then use the add_filter
function to hook this function to the login_errors
filter. This ensures that our custom error message will be displayed instead of the default message.
You can place this code snippet in your theme’s functions.php
file or in a custom plugin file. After adding the code, the login error message will be replaced with your custom message.
This code snippet can be useful in situations where you want to provide a more personalized or informative error message to users who enter incorrect login credentials. It allows you to customize the error message to better suit your website’s branding or provide specific instructions to users.
Examples
Example 1: Change the default login error message
This use case demonstrates how to change the default error message displayed on the WordPress login page when a user enters incorrect login credentials.
function wpsnippets_custom_login_error_message() {
return 'Oops! The username or password you entered is incorrect. Please try again.';
}
add_filter( 'login_errors', 'wpsnippets_custom_login_error_message' );
The code example above uses the login_errors
filter to modify the default error message. The wpsnippets_custom_login_error_message
function returns the desired custom error message, which will be displayed instead of the default one.
Example 2: Customize the login error message based on user role
This use case demonstrates how to customize the login error message based on the user role. For example, you may want to display a different error message for administrators and subscribers.
function wpsnippets_custom_login_error_message() {
if ( current_user_can( 'administrator' ) ) {
return 'Oops! You are not authorized to access the admin area.';
} elseif ( current_user_can( 'subscriber' ) ) {
return 'Oops! Your account is inactive. Please contact the site administrator.';
} else {
return 'Oops! The username or password you entered is incorrect. Please try again.';
}
}
add_filter( 'login_errors', 'wpsnippets_custom_login_error_message' );
In this code example, the wpsnippets_custom_login_error_message
function checks the user’s role using the current_user_can
function. Depending on the user’s role, a different error message is returned.
Example 3: Translate the login error message
This use case demonstrates how to translate the login error message into different languages using WordPress localization functions.
function wpsnippets_custom_login_error_message() {
return __( 'Oops! The username or password you entered is incorrect. Please try again.', 'text-domain' );
}
add_filter( 'login_errors', 'wpsnippets_custom_login_error_message' );
The code example above uses the __
function, which is a localization function in WordPress. By wrapping the error message in the __
function and specifying the text domain, you can easily translate the error message into different languages using translation files.