Lock out an IP address for 15 minutes after 5 failed logins, using transients, with no extra tables.
const NAHNU_MAX_LOGIN_ATTEMPTS = 5;
const NAHNU_LOCKOUT_MINUTES = 15;
function nahnu_login_attempts_key(): string {
return 'nahnu_ll_' . md5( (string) ( $_SERVER['REMOTE_ADDR'] ?? '' ) );
}
// Refuse logins from locked-out addresses, even with the right password.
add_filter( 'authenticate', function ( $user ) {
if ( (int) get_transient( nahnu_login_attempts_key() ) >= NAHNU_MAX_LOGIN_ATTEMPTS ) {
return new WP_Error( 'nahnu_locked_out', sprintf( '<strong>Error:</strong> Too many failed logins. Please try again in %d minutes.', NAHNU_LOCKOUT_MINUTES ) );
}
return $user;
}, 30 );
// Count failures per IP address.
add_action( 'wp_login_failed', function (): void {
$key = nahnu_login_attempts_key();
set_transient( $key, (int) get_transient( $key ) + 1, NAHNU_LOCKOUT_MINUTES * MINUTE_IN_SECONDS );
} );
// Reset after a successful login.
add_action( 'wp_login', function (): void {
delete_transient( nahnu_login_attempts_key() );
} );
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.
By default WordPress lets anyone try passwords as fast as they like. This snippet counts failed logins per IP address and blocks that address for 15 minutes after five failures.
Each failed login, from wp-login.php, WooCommerce’s login form or anything else using wp_signon(), triggers wp_login_failed, which adds one to a counter stored in a transient keyed by a hash of the IP address. The transient expires after the lockout time.
The authenticate filter runs at priority 30, after WordPress has checked the password. If the address has reached the limit, it returns an error even when the password is right, so the attacker can’t use the lockout window to keep guessing. A successful login clears the counter.
REMOTE_ADDR is the proxy’s address, so everyone would share one counter. Have your host restore real visitor IPs first._transient_nahnu_ll_ rows from the options table.In a private window, enter a wrong password five times. The sixth attempt, even with the right password, should show the “Too many failed logins” message.