MailChimp checkbox in WordPress

I try to add a checkbox to my WordPress comment form. Once this is clicked and the comment has been submitted, the mail address should be transferred to mailchimp.

I tried it with this code, funny thing is that the comment status is changed to “subscribed” but in MailChimp I can not see any new email address after my testing. Does anybody see whats missing here?

function custom_add_meta_settings($comment_id) {
  add_comment_meta(
    $comment_id,
    'mailchimp_subscribe',
    $_POST['mailchimp_subscribe'],
    true );
}
add_action ('comment_post', 'custom_add_meta_settings', 1);

function custom_subscription_add( $cid, $comment ) {
  $cid = (int) $cid;
  if ( !is_object($comment) )
    $comment = get_comment($cid);
  if ( $comment->comment_karma == 0 ) {
        $subscribe = get_comment_meta($cid, 'mailchimp_subscribe', true);
        if ( $subscribe == 'on' ) {
            $apikey   = '01d11fa0bhhg15d696985cbb8b812451-us10';
            $listid   = '1d9a4d2g6a';
            $endpoint   = 'https://us10.api.mailchimp.com/2.0/?output=php';
            $request   = array(
              'apikey' => $apikey,
              'id' => $listid,
              'email_address' => strtolower( $comment->comment_author_email ),
              'double_optin' => true,
              'merge_vars' => array(
                'NAME' => $comment->comment_author,
                'OPTIN_IP' => $comment->comment_author_IP,
              )
            );
            $result = wp_remote_post(
              $endpoint.'&method=listSubscribe',
              array( 'body' => json_encode($request) )
            );
            if ( !is_wp_error( $result ) ) {
                update_comment_meta($cid, 'mailchimp_subscribe', 'subscribed', 'on');
            }//if
        }//if
  }//if
}//function
add_action('comment_approved_','custom_subscription_add',10,1);
add_action('comment_post', 'custom_subscription_add', 60,1);

add_action( 'comment_form', 'custom_comment_form_fields' );
function custom_comment_form_fields( $fields ) {
    echo '<p class="comment-mailchimp-form"><input type="checkbox" name="mailchimp_subscribe" id="mailchimp_subscribe"/><label for="mailchimp_subscribe">Get my newsletter</label></p>';
}//function

Related posts

Leave a Reply