Stop Storing IP Address in WordPress Comments or Limit the Storage Time

The storage of IP addresses of blog commenters is a default WordPress feature. It helps website administrators fight comment spam. Akismet uses this data to block comments from suspicious IP addresses.

If you prefer to moderate blog comments individually and don’t need IP addresses, then you can definitely remove them from your WP database. But please be careful and only implement it when you are sure.

In this article, I will show you several ways to prevent the storage of IP addresses, delete existing IPs from the database, or limit the storage period (for example, 7, 14, 30 or 60 days).

Note: Make sure you backup your database before you make any changes!

1. Prevent storage of IP addresses

You can prevent WordPress from storing IP addresses of commenters relatively easily.

You can accomplish this effortlessly by installing and activating the Remove IP plug-in.

But if you don’t want to use an additional plugin, just paste the following code into your active theme’s functions.php:

function bm_remove_ip ( $ comment_author_ip ) { return '' ; } 
add_filter ( 'pre_comment_user_ip' , 'bm_remove_ip' );

Using this method, all commenters’ IP addresses will be replaced with (which is commonly used for localhost). The code used here is basically the same as that used by the “Remove IP” plugin.

You can, however, delete the data completely by pasting the following code into your theme’s functions.php file:

function wpb_remove_commentsip ( $ comment_author_ip ) {
return statement '' ;
add_filter ( 'pre_comment_user_ip' , 'wpb_remove_commentsip' );

The code snippet only prevents future IP addresses from being stored. The next step is to remove existing IP addresses from the database.

2. Delete existing IP addresses from the database

To remove existing IP addresses from the database, use the Remove Comment IPs plugin. As soon as the plugin is activated, all old IP addresses will be automatically deleted from the database. But you may have to wait a while depending on how big your WordPress database is.

It is important to note that if you disable WP-Cron, the plugin may not work. If WP Cron is disabled, you must start it manually or reactivate it on temporary basis.

Alternatively, if you are comfortable editing MySQL databases, you can remove IP addresses using a simple command in PHPMyAdmin:

UPDATE wp_comments SET comment_author_IP = ''

Note: If you used a different database prefix than wp_, the command should be edited accordingly. Replace “wp” with your own prefix.

3. Limit storage duration to X days

What if you want to limit the storage duration of IP addresses rather than completely delete them? You can limit storage duration with the plugin “Remove Comment IPs”. The plugin automatically deletes IP addresses after 60 days when activated.

Unfortunately, the plugin does not allow you to adjust the duration. But you can do this by making a small change to the plugin source code.

To do this, go to Plugins> Editor inside the WordPress dashboard and select “Remove Comment IPs” from the top right corner drop down menu. Then click on the plugin file remove-comment-ips.php and look for the following code block:

function remove_comment_ip_schedule_future_processing ( $ comment_id , $ comment_approved ) { // schedule processing in 60 days 
  wp_schedule_single_event ( time () + 5184000 , 'remove_comment_ip_handle' , array ( $ comment_id )); } 
add_action ( 'comment_post' , 'remove_comment_ip_schedule_future_processing' , 10 , 2 );

There you will find the preset 60 days in the form of 5184000 seconds. Replace it with your desired time duration.

For 7 days you have to enter 7 * 24 * 60 * 60 = 604800 seconds; for 14 days this would be 14 * 24 * 60 * 60 = 1209600 seconds, and so on.

Note: Backing up your WordPress installation may affect the actual storage time of IP addresses, since they will continue to appear in your backups even after being deleted.

If you liked this post, please consider sharing it with your friends:


Leave a Comment