# Newsletter Opt-In Checkbox on the Comment Form

> Adds an unticked "Send me the newsletter" checkbox to the comment form and lets you download the email addresses of commenters who opted in as a CSV from Tools.

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

## Code

```php
add_filter( 'comment_form_submit_field', function ( $field ) {
    return '<p class="comment-form-newsletter"><input type="checkbox" id="wpide-comment-newsletter" name="wpide_comment_newsletter" value="1"> <label for="wpide-comment-newsletter">Send me the newsletter</label></p>' . $field;
}, 5 );

add_action( 'comment_post', function ( $comment_id ) {
    if ( ! empty( $_POST['wpide_comment_newsletter'] ) ) {
        add_comment_meta( $comment_id, 'wpide_newsletter_optin', gmdate( 'Y-m-d H:i:s' ), true );
    }
} );

add_action( 'admin_menu', function () {
    add_management_page( 'Comment Opt-Ins', 'Comment Opt-Ins', 'manage_options', 'wpide-comment-optins', function () {
        global $wpdb;
        $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT c.comment_author_email) FROM {$wpdb->comments} c INNER JOIN {$wpdb->commentmeta} m ON m.comment_id = c.comment_ID WHERE m.meta_key = %s AND c.comment_approved = '1'", 'wpide_newsletter_optin' ) );

        echo '<div class="wrap"><h1>Comment Opt-Ins</h1>';
        printf( '<p>%d commenter(s) asked to receive the newsletter.</p>', (int) $count );
        printf( '<p><a class="button button-primary" href="%s">Download CSV</a></p></div>', esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=wpide_comment_optins' ), 'wpide_comment_optins' ) ) );
    } );
} );

add_action( 'admin_post_wpide_comment_optins', function () {
    global $wpdb;

    if ( ! current_user_can( 'manage_options' ) || ! check_admin_referer( 'wpide_comment_optins' ) ) {
        wp_die( 'Not allowed.' );
    }

    $rows = $wpdb->get_results( $wpdb->prepare( "SELECT c.comment_author_email AS email, MAX(c.comment_author) AS name, MAX(m.meta_value) AS opted FROM {$wpdb->comments} c INNER JOIN {$wpdb->commentmeta} m ON m.comment_id = c.comment_ID WHERE m.meta_key = %s AND c.comment_approved = '1' GROUP BY c.comment_author_email", 'wpide_newsletter_optin' ) );

    nocache_headers();
    header( 'Content-Type: text/csv; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename=comment-optins-' . gmdate( 'Y-m-d' ) . '.csv' );

    $out = fopen( 'php://output', 'w' );
    fputcsv( $out, array( 'email', 'name', 'opted_in' ) );
    foreach ( $rows as $row ) {
        fputcsv( $out, array( ltrim( $row->email, '=+-@' ), preg_replace( '/^[=+\-@]+/', '', (string) $row->name ), $row->opted ) );
    }
    fclose( $out );
    exit;
} );
```

## Notes

Only commenters who tick the box are exported (from Tools > Comment Opt-Ins); import the CSV into your mailing tool, which should handle confirmation.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Newsletter Opt-In Checkbox on the Comment Form" 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/
