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);
});