Require visitors to tick "I agree to the privacy policy" before their comment is accepted.
add_filter( 'comment_form_default_fields', function ( array $fields ): array {
$policy = get_privacy_policy_url();
$label = $policy
? sprintf( 'I have read the <a href="%s" target="_blank">privacy policy</a> and agree to my name, email and comment being stored.', esc_url( $policy ) )
: 'I agree to my name, email and comment being stored.';
$fields['nahnu_privacy'] = sprintf(
'<p class="comment-form-nahnu-privacy"><input id="nahnu_privacy" name="nahnu_privacy" type="checkbox" value="1" required> <label for="nahnu_privacy">%s</label></p>',
wp_kses( $label, [ 'a' => [ 'href' => [], 'target' => [] ] ] )
);
return $fields;
} );
// Check it on the server too. Runs only for comments submitted through the form.
add_action( 'pre_comment_on_post', function (): void {
if ( ! is_user_logged_in() && empty( $_POST['nahnu_privacy'] ) ) {
wp_die(
'Please tick the privacy box to post your comment.',
'Privacy consent needed',
[ 'response' => 400, 'back_link' => true ]
);
}
} );
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.
If you need recorded consent before storing commenters’ personal data, a required checkbox does the job. This snippet adds one with a link to your privacy policy page.
comment_form_default_fields adds the checkbox to the comment form for logged-out visitors. It uses the privacy policy page set under Settings → Privacy, and the required attribute stops the browser sending the form without it.
Browsers can be bypassed, so pre_comment_on_post checks again on the server. It runs only when a comment is submitted through wp-comments-post.php, and stops with a friendly message if the box wasn’t ticked.
Log out, write a comment and submit it without ticking the box. The browser should stop you. With the box ticked, the comment should post.