Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.