Security PHP Intermediate

Limit Login Attempts

Lock out an IP address for 15 minutes after 5 failed logins, using transients, with no extra tables.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 25 lines
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() );
} );

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.

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.

How it works

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.

Things to watch

  • Each blocked attempt during a lockout counts as another failure and restarts the 15 minutes, which keeps persistent bots locked out.
  • Behind Cloudflare or a proxy, REMOTE_ADDR is the proxy’s address, so everyone would share one counter. Have your host restore real visitor IPs first.
  • If you lock yourself out, wait 15 minutes or delete the _transient_nahnu_ll_ rows from the options table.

How to test it

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.

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