• Resolved FutureX5

    (@futurex5)


    I’m trying to add a specific MC tag when a user posts a WP comment in a specific category. I’ve tried the info on this page:

    https://wordpress.org/support/topic/add-different-tags-to-every-wpforms-form/

    but can’t get it to recognize the category. This is the code I want to work:

    add_filter( ‘mc4wp_integration_wp-comment-form_subscriber_data’, ‘myprefix_woocommerce_subscriber_data’ );
    function myprefix_woocommerce_subscriber_data( MC4WP_MailChimp_Subscriber $subscriber ) {
    if ( in_category( ‘caption-contest’ ) ) {
    $subscriber->tags[] = ‘Caption Contest’;
    }
    return $subscriber;
    }

    If I remove the ‘if statement’ it works fine but I want to restrict it to the one category.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Contributor Lap

    (@lapzor)

    I think that this code runs outside of the post loop so it doesn’t know what post we’re at and what it’s category is.

    The mc4wp_integration_wp-comment-form_subscriber_data filter has $related_object_id as second parameter after the $subscriber.

    I believe that would be the post ID in this case, that you can use to look up the post and get it’s category.

    Kind regards,

    Thread Starter FutureX5

    (@futurex5)

    Thanks for the reply!

    How do I pull out the $related_object_id from mc4wp_integration_wp-comment-form_subscriber_data?

    Then I can use:

    $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;
    
    Plugin Contributor Lap

    (@lapzor)

    Something like this (untested example code).

    add_filter(
    	'mc4wp_integration_wp-comment-form_subscriber_data’',
    	function( MC4WP_MailChimp_Subscriber $subscriber, $postid ) {
    		//your code here using $postid to get the id number of the post
    		return $subscriber
    	},10,2);
    Thread Starter FutureX5

    (@futurex5)

    Thanks for helping! Here’s what I tried which still does not work. It does register the user with MailChimp fine but does not set the tag.

    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);
    Thread Starter FutureX5

    (@futurex5)

    Where is:

    mc4wp_integration_wp-comment-form_subscriber_data

    located?

    Plugin Contributor Lap

    (@lapzor)

    As a test can you write $postid to a text field in Mailchimp just to see if that ID is coming in properly?

    Kind regards,

    Thread Starter FutureX5

    (@futurex5)

    That second field is not Post ID but List ID so it’s of no use. I’ve tried everything I can think of to get his work but nothing works. It seems like it should be very simple but once the MC4MP functions are called all the data is gone. I tried global variables to read the category before the comment is sent, no luck. I tried reading the $_POST data but not luck even in global variable, and many many other iterations. 🙁 Would be a nice feature.

    Plugin Contributor Lap

    (@lapzor)

    oh i see, sorry about that.

    The hook happens outside of the post loop so the only other way that I can then think of is creating a hidden field with the post ID in it so that you have the post ID in the $_POST data.

    Thread Starter FutureX5

    (@futurex5)

    But if a global variable that is set before MC4MP even runs won’t work how would this work? Outside of the MC4MP function it works perfectly, I can test for category and do whatever I want.

    Plugin Contributor Lap

    (@lapzor)

    Plugin Contributor Lap

    (@lapzor)

    The mc4wp_integration_wp-comment-form_data hook receives the comment ID as a parameter. This comment ID can be used to retrieve any related information on the comment, as follows:

    add_action( 'mc4wp_integration_wp-comment-form_data', function( $data, $comment_id ) {
    $comment = get_comment( $comment_id );
    $post_id = $comment->comment_post_ID;
    // …. etc
    }, 10, 2);

    Hope that helps. If you have any questions, please let me know!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Add MC Tag Based On Post Category’ is closed to new replies.