Security PHP Beginner

Show One Generic Login Error

Replace "The password you entered for the username admin is incorrect" with one message that doesn't confirm whether a username exists.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 9 lines
add_filter( 'login_errors', function ( string $error ): string {
	global $errors;
	$codes = $errors instanceof WP_Error ? $errors->get_error_codes() : [];

	if ( array_intersect( $codes, [ 'invalid_username', 'invalid_email', 'incorrect_password' ] ) ) {
		return '<strong>Error:</strong> The username or password is incorrect.';
	}
	return $error;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress’s login errors are specific: one message for an unknown username and another for a wrong password. That tells attackers which usernames exist. This snippet replaces both with one generic message.

How it works

The login_errors filter receives the error HTML shown on wp-login.php. The snippet checks the error codes WordPress set and only replaces the username and password ones, so other messages, like “you are now logged out” or lockout notices, still appear.

Things to watch

  • The lost password form can still reveal whether an account exists. Combine this with a security plugin if that matters for your site.
  • WooCommerce’s My Account login form has its own messages and isn’t affected.

How to test it

On the login page, enter a username that doesn’t exist, then a real username with a wrong password. Both should show the same message.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only