Skip to main content
Login Join
Snippet · PHP

Limit Login Attempts Without Plugin

Shared by Darshan Chauhan · May 2, 2026

13 views
1 upvote
Back to Snippets

Prevents brute-force attacks by limiting failed login attempts per IP using WordPress transients.

function limit_login_attempts() {
    $ip = $_SERVER['REMOTE_ADDR'];
    $key = 'login_attempts_' . $ip;

    $attempts = get_transient($key);

    if ($attempts && $attempts >= 5) {
        wp_die('Too many login attempts. Please try again later.');
    }
}
add_action('login_init', 'limit_login_attempts');

add_action('wp_login_failed', function() {
    $ip = $_SERVER['REMOTE_ADDR'];
    $key = 'login_attempts_' . $ip;

    $attempts = get_transient($key);
    $attempts = $attempts ? $attempts + 1 : 1;

    set_transient($key, $attempts, 15 * MINUTE_IN_SECONDS);
});
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments