Privacy PHP Beginner

Stop Saving Comment Cookies

Stop WordPress saving commenters' name, email and website in cookies, and remove the "Save my name" checkbox.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 8 lines
add_action( 'init', function (): void {
	remove_action( 'set_comment_cookies', 'wp_set_comment_cookies' );
} );

add_filter( 'comment_form_default_fields', function ( array $fields ): array {
	unset( $fields['cookies'] );
	return $fields;
} );

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.

When someone comments, WordPress can save their name, email and website in cookies for a year so the form is pre-filled next time. If you don’t want to set those cookies, or list them in your cookie policy, this snippet turns them off.

How it works

WordPress sets the cookies from wp_set_comment_cookies(), hooked to set_comment_cookies. Removing it means no comment cookies are ever saved.

The consent checkbox under the comment form is removed with comment_form_default_fields, since there’s nothing to agree to.

Things to watch

  • Returning commenters will need to type their details again.
  • Cookies already saved in visitors’ browsers stay until they expire.

How to test it

Leave a comment while logged out. Your browser’s cookies should have no comment_author_ entries, and the comment form should have no “Save my name” box.

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