Select Page

Do you want to stop storing the IP Addresses of your website users who leave comments?

Here’s the code you need. Place it in your WordPress theme’s functions.php file:

/**
 * Custom function to stop storing IP addresses in WordPress comments
 *
 * @param array $commentdata An array of comment data
 *
 * @return array $commentdata The modified array of comment data
 */
function stop_storing_ip_addresses( $commentdata ) {
  // Set the comment IP address to an empty string
  $commentdata['comment_author_IP'] = '';
  return $commentdata;
}
add_filter( 'pre_comment_user_ip', 'stop_storing_ip_addresses', 10, 1 );

The ‘stop_storing_ip_addresses’ function is a custom function that takes one argument: $commentdata. The $commentdata argument is an array of comment data, including the comment author’s IP address.

Inside the function, the ‘comment_author_IP’ element of the $commentdata array is set to an empty string. This removes the IP address from the comment data. Finally, the function is hooked to the ‘pre_comment_user_ip’ filter using the ‘add_filter’ function, which applies the changes to the comment data before storing it in the database.

This code will stop storing IP addresses in WordPress comments on your site.