Keep WordPress comment_agent and comment_user_IP empty with hooks

WordPress saves all kinds of information when someone leaves a comment on your blog/website. Two pieces of information I don’t need from someone who comments on an article are the IP-address of the author and the User Agent of the browser they used to place the comment.

pre_comment_* hooks

WordPress has pre_comment_* hooks which can be used to alter data before it’s put into the database.

I used the following two hooks:

pre_comment_user_ip

This hook filters the comment author’s IP before it is set, so this can be used to alter the IP-address before it’s saved in the database.

// Don't log IP addresses of comment authors.
add_filter( 'pre_comment_user_ip', '__return_zero' );

The filter uses the function __return_zero, which does exactly as it says: the comment_user_ip is set to 0.

Clean up already saved IP-addresses

To clean up the already saved IP-addresses, you execute the following SQL-query (after you made sure you have made a full backup of the database) via phpMyAdmin to your WordPress database:

update wp_comments set comment_author_IP ='' ;

pre_comment_user_agent

This hook filters the comment author’s browser user agent before it is set, so this can be used to alter the User Agent before it’s saved in the database.

// Don't log User Agents of comment authors.
add_filter( 'pre_comment_user_agent', '__return_empty_string' );

The filter uses the function __return_empty_string, which returns an empty string before the data is saved into the database, thus keeping the database clean from unneeded information.

Clean up already saved User Agents

To clean up the already saved User Agents, you execute the following SQL-query (after you made sure you have made a full backup of the database) via phpMyAdmin to your WordPress database:

update wp_comments set comment_agent ='' ;