Record every failed login with username, IP address and browser in a small database table, and clear out entries older than 90 days automatically.
/**
* Log failed logins to a custom table and keep 90 days of history.
*/
function nahnu_login_log_table(): string {
global $wpdb;
return $wpdb->prefix . 'nahnu_login_failures';
}
// Create the table once, not on every page load.
add_action( 'admin_init', function (): void {
if ( '1' === get_option( 'nahnu_login_log_db' ) ) {
return;
}
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( 'CREATE TABLE ' . nahnu_login_log_table() . " (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
username varchar(200) NOT NULL DEFAULT '',
ip_address varchar(45) NOT NULL DEFAULT '',
user_agent varchar(255) NOT NULL DEFAULT '',
attempted datetime NOT NULL,
PRIMARY KEY (id),
KEY ip_address (ip_address),
KEY attempted (attempted)
) {$wpdb->get_charset_collate()};" );
update_option( 'nahnu_login_log_db', '1', false );
} );
add_action( 'wp_login_failed', function ( string $username ): void {
global $wpdb;
// REMOTE_ADDR can't be faked. If you're behind Cloudflare or a proxy,
// see the notes for how to use the forwarded address safely.
$ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) );
$wpdb->insert( nahnu_login_log_table(), [
'username' => sanitize_user( $username ),
'ip_address' => filter_var( $ip, FILTER_VALIDATE_IP ) ? $ip : '',
'user_agent' => mb_substr( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ), 0, 255 ),
'attempted' => current_time( 'mysql', true ),
], [ '%s', '%s', '%s', '%s' ] );
} );
// Delete entries older than 90 days, once a day.
add_action( 'init', function (): void {
if ( ! wp_next_scheduled( 'nahnu_login_log_cleanup' ) ) {
wp_schedule_event( time(), 'daily', 'nahnu_login_log_cleanup' );
}
} );
add_action( 'nahnu_login_log_cleanup', function (): void {
global $wpdb;
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . nahnu_login_log_table() . ' WHERE attempted < %s', gmdate( 'Y-m-d H:i:s', time() - 90 * DAY_IN_SECONDS ) ) );
} );
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.
WordPress doesn’t keep any record of failed logins. This snippet logs each one, with the username tried, the IP address, the browser and the time, so you can see whether you’re being targeted and which addresses to block.
The table is created with dbDelta() the first time an administrator loads wp-admin, and an option records that it exists. The previous version ran the table check on every single page load, which added needless database work to every visit.
The wp_login_failed action fires for every failed login, including WooCommerce’s login form and XML-RPC. Each row stores times in UTC, and a daily scheduled task deletes anything older than 90 days so the table never grows forever and you don’t keep personal data longer than you need.
The snippet uses REMOTE_ADDR, the only address a visitor can’t fake. Headers like X-Forwarded-For are sent by the client, so an attacker can put any address in them. If your site sits behind Cloudflare, REMOTE_ADDR will be a Cloudflare address. In that case, only trust HTTP_CF_CONNECTING_IP after checking that REMOTE_ADDR is in Cloudflare’s published IP ranges, or have your host restore the real visitor IP at the server level.
Query the table with a database tool, or with WP-CLI: wp db query "SELECT ip_address, COUNT(*) AS tries FROM wp_nahnu_login_failures GROUP BY ip_address ORDER BY tries DESC LIMIT 20". Swap wp_ for your table prefix.
IP addresses are personal data under the GDPR. Mention security logging in your privacy policy, and shorten the 90 days if you don’t need that long.