Privacy PHP Beginner

Stop Storing the Browser Details of Commenters

Stop WordPress saving each commenter's browser and device details (user agent) with their comment.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 1 lines
add_filter( 'pre_comment_user_agent', '__return_empty_string' );

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 full browser user agent string of everyone who comments, like “Mozilla/5.0 (iPhone; CPU iPhone OS 18_1…”. It’s rarely useful and counts as extra personal data. This one line stops it being saved.

How it works

Before a comment is saved, its user agent goes through pre_comment_user_agent. Returning an empty string means nothing is stored in the comment_agent column.

Things to watch

  • Some anti-spam services use the user agent. Akismet still receives it when checking the comment, because it reads it from the request, not the database.
  • Existing comments keep their stored user agent.

How to test it

Leave a test comment, then check its comment_agent value in the comments table. It should be empty.

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