Wordpress Snippets

Disable Storing IP Address in WordPress Comments

While posting a comment, WordPress automatically stores commenter’s IP address along with other information in MySQL database. Storing IP address is useful when you want to identify comment author for any unlawful/illegal content or a comment spammers to prevent them for posting anymore spam comments. Although this is a useful feature but in some countries, law forbids storing users IP addresses permanently. Typically these should not be stored any longer than seven days, if it is not required to identify the author for any unlawful/illegal content. Apart from that it’s also a good idea to keep database clean and tidy. So in this article, I will show you how to remove stored IP addresses from database and also if you like, how to prevent storing IP address in first place.

There are a couple of plugins available in WordPress plugin repository to disable logging / storing IP addresses on comments but why use plugin when we can accomplish this simple task by inserting small code in theme files. Just paste this WordPress code snippet in your functions.php file and storing IP address will be permanently disabled on WordPress comments. Although you will still have the comment_author_IP field in the database, but it will remain empty.

// disable storing ip address in wordpress comments
function wcs_no_ip_comments( $comment_author_ip ) {
    return '';
}
add_filter( 'pre_comment_user_ip', 'wcs_no_ip_comments' );

This method will prevent storing IP address of commenter in all future comments but it will not remove existing IP addressed stored in the database. To remove existing IP records from the MySQL database simply run this query in phpMyAdmin.

UPDATE 'wp_comments' SET 'comment_author_IP' = '';

Note: While the above MySQL query has been tested, but you should definitely not try it without first having a proper backup of your MySQL database.

Hire Us. Or just say Hi!
Need a job? Apply to get one.