Skip to content

Limit Login Attempts in WordPress Without a Plugin

Limit login attempts in WordPress without a plugin is possible, and the honest version takes about forty lines of code. This guide shows a limit login attempts WordPress without plugin setup, and names the four gaps it leaves. The question is whether the result is worth the maintenance, and where a hand-written counter stops working.

This is the do-it-yourself path, with the gaps named, so the choice between code and a plugin is made on facts.

What the attempt counter does

The login form accepts a POST request and answers “wrong password” as many times as anyone asks. A counter changes one thing: after N failures from the same source, the answer becomes a delay, a challenge or a block.

Three decisions follow. What counts as a source: address, subnet or account name. What counts as a failure: only the password, or the second factor too. What happens next: a pause, a CAPTCHA, or a ban with an expiry.

Without those decisions the code is a guess, and the guesses are why people install a plugin in the end.

The minimal version in code

The shortest working version hooks wp_login_failed and authenticate, keeps a counter in a transient keyed by the address, and returns an error once the limit is reached.

add_action( 'wp_login_failed', function ( $user ) {
    $ip  = $_SERVER['REMOTE_ADDR'];
    $key = 'fail_' . md5( $ip );
    $n   = (int) get_transient( $key ) + 1;
    set_transient( $key, $n, 15 * MINUTE_IN_SECONDS );
} );

add_filter( 'authenticate', function ( $user ) {
    $ip  = $_SERVER['REMOTE_ADDR'];
    $key = 'fail_' . md5( $ip );
    if ( (int) get_transient( $key ) >= 5 ) {
        return new WP_Error( 'too_many', 'Too many attempts. Try again later.' );
    }
    return $user;
}, 30 );

This blocks the fifth attempt for fifteen minutes on one address. It is real protection against a single script and nothing against a botnet.

Four gaps a short snippet leaves

Addresses rotate. Ten attempts per address looks strict until the attacker has a proxy pool. The counter must also run per subnet, which means working with CIDR ranges and IPv6, not a string key.

Headers lie. REMOTE_ADDR is honest only when the site is not behind a proxy. Behind a CDN the real address arrives in X-Forwarded-For, and trusting that header blindly lets a script choose its own counter. Correct handling needs a configured list of trusted proxies.

XML-RPC and REST are other doors. wp-login.php is one entry point. XML-RPC can carry hundreds of attempts in one request, and the REST API checks application passwords on its own path. A counter on the form leaves both open, and closing them is a separate decision.

The admin is locked out too. An office behind one address, a password manager and a person who forgot which password belongs to which site: the counter bans the owner. Expiry, a slow path before the ban, and a way to clear the record are not optional.

What the code does not cover

The counter cannot see a correct password from a clean address. That case belongs to the second factor, and TOTP is a separate piece of work: shared secret, QR enrollment, backup codes and a recovery path.

The counter also does not log. When a client asks why the site refused a login, “the transient said five” is not an answer. A usable setup writes each failure with the address, the account and the rule, and keeps it long enough to read.

Where the plugin case becomes obvious

Add subnet counting, trusted proxy handling, XML-RPC coverage, logging, expiry management and a settings screen that a client can read, and the forty lines become a small project with a maintenance bill.

That is the moment BotBlocker stops being a luxury. It ships the ladder: five failures in fifteen minutes to the slow path, a CAPTCHA around thirty requests per minute, a ban at fifty, per address and per subnet. XML-RPC and REST are covered by the same counter, and the event log keeps 55 codes in 12 categories.

The code path stays valid for one case: a site with a single static office address and no CDN, where the owner writes PHP for a living. For everyone else the plugin is cheaper than the maintenance.

What to test before choosing

Send six wrong passwords from one address and watch what happens on the sixth. Send a batch through XML-RPC and see whether the counter notices. Check whether the ban expires. Then ask who fixes the counter when WordPress changes the authentication hook.

Next step

Start with the minimal snippet only if you can own it. The brute force protection page lists the defaults, the brute-forcing guide explains the attack, and the prices name the tier. WordPress documents the hooks in its reference.