Privacy PHP Intermediate

Auto-Anonymize Old Comment IP Addresses

Automatically anonymize the stored IP addresses of comments older than a configurable number of days, reducing personal data retention without deleting the comments themselves.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 17 lines
// Clear the IP address from comments older than 90 days.
function nahnu_anonymize_old_comment_ips( int $days = 90 ): int {
	global $wpdb;
	$cutoff = gmdate( 'Y-m-d H:i:s', time() - $days * DAY_IN_SECONDS );
	return (int) $wpdb->query( $wpdb->prepare(
		"UPDATE {$wpdb->comments} SET comment_author_IP = '' WHERE comment_date_gmt < %s AND comment_author_IP <> ''",
		$cutoff
	) );
}

// Run it once a week.
add_action( 'nahnu_weekly_ip_anonymize', 'nahnu_anonymize_old_comment_ips' );
add_action( 'init', function (): void {
	if ( ! wp_next_scheduled( 'nahnu_weekly_ip_anonymize' ) ) {
		wp_schedule_event( time(), 'weekly', 'nahnu_weekly_ip_anonymize' );
	}
} );

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.

WordPress stores the IP address of every comment author in the wp_comments table. Under GDPR and similar privacy regulations, IP addresses are considered personal data, they can be used to identify an individual, particularly when combined with a timestamp and other identifying information like a name and email. Retaining comment IP addresses indefinitely is a data minimisation issue: you may need the IP for spam moderation shortly after a comment is submitted, but there’s rarely a justification for keeping it for years.

This snippet schedules a weekly cleanup that anonymizes IPs from comments older than a configurable threshold by replacing them with an empty string, preserving the comment content while removing the personal identifier.

What the code does

The cleanup runs automatically via WP-Cron without any manual intervention after setup.

The Anonymization Query

The direct database query updates all rows in wp_comments where the comment date is before the cutoff and the IP address field is not already empty (to avoid unnecessary updates on already-anonymized records). It uses a prepared statement with $wpdb->prepare() for safety, and the query is deliberately simple and index-friendly, comment_date_gmt is an indexed column in WordPress’s default schema.

The function returns the number of updated rows, which is useful for logging or confirmation if you run it manually.

Choosing the Retention Period

The default of 90 days gives you three months to use the IP for spam analysis, which is more than enough for any practical moderation workflow. Under GDPR, data should be kept only as long as there’s a legitimate purpose, for comment IPs, that purpose (spam moderation and abuse prevention) typically expires within 30–60 days. Adjust the $days_old default in the function signature to match your site’s data retention policy.

Running It Manually

To anonymize existing old IPs immediately without waiting for the cron schedule, call nahnu_anonymize_old_comment_ips() directly, from a temporary script, from WP-CLI (wp eval 'nahnu_anonymize_old_comment_ips();'), or from a one-time admin page. After the initial run, the weekly cron handles ongoing cleanup automatically.

Privacy Policy

If your site’s privacy policy documents comment data retention, update it to reflect the anonymization schedule. Stating that IP addresses associated with comments are anonymized after 90 days is a concrete, documentable data minimisation practice that demonstrates compliance intent.

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