Prevent brute force attacks on the WordPress login with rate limiting
The WordPress login page is a constant target of bots that try thousands of username and password pairs. Without limits WordPress checks every attempt: PHP workers are busy, the database gets user lookups, and sooner or later a weak password is found. Protection works in layers: limit failed attempts per IP in PHP, and, where possible, let only known IPs reach wp-login.php at all.
Problem: The login page receives hundreds of automated attempts per minute and has no lockout.
Solution: Count failed logins per IP in a transient on wp_login_failed, and on the authenticate filter reject the login while the IP is locked. Optionally restrict wp-login.php to your IPs in .htaccess.
The lockout
In a plugin or functions.php:
<?php
const HA_LOGIN_MAX_FAILS = 5;
const HA_LOGIN_LOCK_TIME = 15 * MINUTE_IN_SECONDS;
function ha_login_lock_key() {
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '0.0.0.0';
return 'ha_login_fail_' . md5( $ip );
}
// Count failed attempts
add_action( 'wp_login_failed', function ( $username ) {
$key = ha_login_lock_key();
set_transient( $key, (int) get_transient( $key ) + 1, HA_LOGIN_LOCK_TIME );
} );
// Reject the login while the IP is locked
add_filter( 'authenticate', function ( $user, $username, $password ) {
if ( '' === $username || (int) get_transient( ha_login_lock_key() ) < HA_LOGIN_MAX_FAILS ) {
return $user;
}
return new WP_Error(
'too_many_attempts',
sprintf( __( 'Too many failed login attempts. Please try again in %d minutes.', 'ha' ), HA_LOGIN_LOCK_TIME / MINUTE_IN_SECONDS )
);
}, 99, 3 );
// A successful login clears the counter
add_action( 'wp_login', function () {
delete_transient( ha_login_lock_key() );
} );
The priority 99 matters. WordPress checks the password itself on authenticate at priority 20, and that check does not keep an error returned by an earlier filter: a WP_Error from priority 1 would simply be replaced by the result of the password check. At 99 our filter has the last word, so a locked IP cannot log in even with the right password. The same filter covers XML-RPC and Application Password logins.
The IP is taken only from REMOTE_ADDR. Headers like X-Forwarded-For are sent by the client, and a bot would put a new random IP into each request to bypass the lockout. If the site is behind a proxy or Cloudflare, configure the web server to put the real client address into REMOTE_ADDR (for example with mod_remoteip and the list of trusted proxy IPs) instead of reading the header in PHP.
Only your IPs on wp-login.php
If the admins always work from fixed IPs, the strongest protection is to keep everyone else away from the login page in .htaccess, so bots never reach PHP:
<Files "wp-login.php">
Require ip 1.2.3.4
Require ip 5.6.7.8
</Files>
Keep in mind that wp-login.php also handles logout, password reset and registration: with this rule, visitors and customers from other IPs cannot use them. For sites with registered users, use the PHP lockout without the IP restriction.
NOTE: The lockout works per IP, so a botnet that uses thousands of addresses is slowed down but not stopped. Strong passwords and two-factor authentication for admin accounts are still the main defence; the lockout mainly reduces the load and the noise. Do not block xmlrpc.php blindly either, see protecting wp-config.php and xmlrpc.php.