# Required Privacy Consent Checkbox on Comments

> Adds a required "I agree to the privacy policy" checkbox to the comment form and rejects comments that are submitted without it, with a link to your privacy page.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: comments, privacy, gdpr
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/form-comment-consent-required/

## Code

```php
add_filter( 'comment_form_submit_field', function ( $field ) {
    if ( is_user_logged_in() ) {
        return $field;
    }

    $policy = get_privacy_policy_url();
    $label  = $policy ? sprintf( 'I agree to the storage of my data as described in the <a href="%s" target="_blank" rel="noopener">privacy policy</a>.', esc_url( $policy ) ) : 'I agree to the storage of my data for handling this comment.';

    return '<p class="comment-form-privacy-consent"><input type="checkbox" id="wpide-comment-consent" name="wpide_comment_consent" value="1" required> <label for="wpide-comment-consent">' . $label . '</label></p>' . $field;
} );

add_filter( 'preprocess_comment', function ( $commentdata ) {
    if ( is_user_logged_in() || ( isset( $commentdata['comment_type'] ) && in_array( $commentdata['comment_type'], array( 'pingback', 'trackback' ), true ) ) ) {
        return $commentdata;
    }

    if ( empty( $_POST['wpide_comment_consent'] ) ) {
        wp_die( 'Please tick the privacy consent box to post your comment.', 'Consent required', array( 'response' => 400, 'back_link' => true ) );
    }

    return $commentdata;
} );
```

## Notes

Links to the privacy policy page set under Settings > Privacy; logged-in users are not asked.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Required Privacy Consent Checkbox on Comments" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
