Try this:
add_filter( 'mc4wp_integration_wp-comment-form_subscriber_data', 'myprefix_woocommerce_subscriber_data' );
function myprefix_woocommerce_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber ) {
$categories = get_the_category();
foreach ( $categories as $category ) {
if ( $category->slug === 'caption-contest' ) {
$subscriber->tags[] = 'Caption Contest';
break;
}
}
return $subscriber;
}
This code retrieves the category of the post that the comment belongs to and checks if it has a slug of “caption-contest”. If it does, it adds the “Caption Contest” tag to the subscriber’s tags array.
Thank you for the reply!
No joy. 🙁 If I strip out the ‘foreach’ and the ‘if’ it works. It doesn’t crash or do anything weird, it just doesn’t set the tag in MailChimp.
okay, break it down barney style for me in terms of what you are trying to achieve. Start with the location that the form/MC is located and if a user is to be logged in or not, etc., etc., as much info as possible.
The form is a standard comment form at the bottom of a regular post. I have MC4WP plugin installed to grab the name and email and send it to MailChimp when the user posts a comment. They don’t have to be logged in. I’m using a child theme, updating the functions.php there. It is the Avada theme.
This code works to move the name/email fields above the comment field:
add_filter( 'comment_form_fields', 'wpb_move_comment_field_to_bottom');
function wpb_move_comment_field_to_bottom( $fields ) {
if ( in_category( 'caption-contest' ) ) {
$comment_field = $fields['comment'];
unset( $fields['comment'] );
$fields['comment'] = $comment_field;
}
return $fields;
}
If your initial code works for all posts and you want to limit it to one category, maybe try the has_category instead:
add_filter( 'mc4wp_integration_wp-comment-form_subscriber_data', 'myprefix_woocommerce_subscriber_data' );
function myprefix_woocommerce_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber ) {
if ( has_category( 'caption-contest' ) ) {
$subscriber->tags[] = 'Caption Contest';
}
return $subscriber;
}
I tried that but for some reason it doesn’t know what category it’s in since it might be outside the Loop.
Here’s what I’ve tried as well that still did not work:
add_filter('mc4wp_integration_wp-comment-form_subscriber_data', function( MC4WP_MailChimp_Subscriber $subscriber, $postid ) {
$category_ids = wp_get_post_categories($post_id);
foreach($category_ids as $cat_id) {
$category = get_category($cat_id);
if($category->name === 'caption-contest') {
$subscriber->tags[] = 'Caption Contest';
}
}
return $subscriber;
},10,2);