From 045bf33506f77d9a06e38d818a7ac0f81334191a Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 10:19:09 +0100 Subject: [PATCH 1/9] Adding tag removal option add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) { $subscriber->tags[] = ['name' => 'removethis', 'status' => 'inactive']; return $subscriber; }); --- includes/class-mailchimp.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/includes/class-mailchimp.php b/includes/class-mailchimp.php index ce3dd76a..62496cff 100755 --- a/includes/class-mailchimp.php +++ b/includes/class-mailchimp.php @@ -91,7 +91,7 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda $data->was_already_on_list = $existing_member_data->status === 'subscribed'; if (isset($args['tags']) && is_array($args['tags'])) { - $this->list_add_tags_to_subscriber($list_id, $data, $args['tags']); + $this->list_tags_to_subscriber($list_id, $data, $args['tags']); } } else { $data = $api->add_new_list_member($list_id, $args); @@ -138,26 +138,43 @@ function ($tag) { } /** - * Post the tags on a list member. + * Post the tags on a list member. * * @param $mailchimp_list_id string The list id to subscribe to * @param $mailchimp_member stdClass mailchimp user informations - * @param $new_tags array tags to add to the user + * @param $tags array tags to set for the user (can include 'status' key) * * @return bool * @throws Exception - * @since 4.7.9 + * @since 4.10.10 */ - private function list_add_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, array $new_tags) + private function list_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, array $tags) { // do nothing if no tags given - if (count($new_tags) === 0) { + if (count($tags) === 0) { return true; } - $api = $this->get_api(); + $api = $this->get_api(); + + // Format tags - handle both string tags (old style, default to active) and array tags with explicit status + $formatted_tags = []; + foreach ($tags as $tag) { + if (is_string($tag)) { + $formatted_tags[] = [ + 'name' => $tag, + 'status' => 'active' + ]; + } elseif (is_array($tag) && isset($tag['name'])) { + $formatted_tags[] = [ + 'name' => $tag['name'], + 'status' => isset($tag['status']) ? $tag['status'] : 'active' + ]; + } + } + $data = [ - 'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $new_tags), + 'tags' => $formatted_tags, ]; try { From 13099b11377973b9cd2b302e3ab3f57263971d6e Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 10:50:59 +0100 Subject: [PATCH 2/9] moved tag array formatting back to its own function --- includes/class-mailchimp.php | 54 ++++++++++++------------------------ 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/includes/class-mailchimp.php b/includes/class-mailchimp.php index 62496cff..34f2a285 100755 --- a/includes/class-mailchimp.php +++ b/includes/class-mailchimp.php @@ -110,31 +110,29 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda * Format tags to send to Mailchimp. * * @param $mailchimp_tags array existent user tags - * @param $new_tags array new tags to add + * @param $tags array new tags to add * * @return array * @since 4.7.9 */ - private function merge_and_format_member_tags($mailchimp_tags, $new_tags) + private function merge_and_format_member_tags($mailchimp_tags, $tags) { - $mailchimp_tags = array_map( - function ($tag) { - return $tag->name; - }, - $mailchimp_tags - ); - - $tags = array_unique(array_merge($mailchimp_tags, $new_tags), SORT_REGULAR); - - return array_map( - function ($tag) { - return [ - 'name' => $tag, - 'status' => 'active', + $formatted_tags = []; + foreach ($tags as $tag) { + if (is_string($tag)) { + $formatted_tags[] = [ + 'name' => $tag, + 'status' => 'active' ]; - }, - $tags - ); + } elseif (is_array($tag) && isset($tag['name'])) { + $formatted_tags[] = [ + 'name' => $tag['name'], + 'status' => isset($tag['status']) ? $tag['status'] : 'active' + ]; + } + } + + return $formatted_tags; } /** @@ -157,24 +155,8 @@ private function list_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, $api = $this->get_api(); - // Format tags - handle both string tags (old style, default to active) and array tags with explicit status - $formatted_tags = []; - foreach ($tags as $tag) { - if (is_string($tag)) { - $formatted_tags[] = [ - 'name' => $tag, - 'status' => 'active' - ]; - } elseif (is_array($tag) && isset($tag['name'])) { - $formatted_tags[] = [ - 'name' => $tag['name'], - 'status' => isset($tag['status']) ? $tag['status'] : 'active' - ]; - } - } - $data = [ - 'tags' => $formatted_tags, + 'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $tags), ]; try { From ef9dc58471a14f60f0043f78de0fc3079039c79b Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 12:38:38 +0100 Subject: [PATCH 3/9] add form setting to remove tagas --- config/default-form-settings.php | 1 + includes/forms/class-form.php | 25 +++++++++++++++++++-- includes/forms/views/tabs/form-settings.php | 14 +++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/config/default-form-settings.php b/config/default-form-settings.php index b2a452a4..1bf4cb55 100755 --- a/config/default-form-settings.php +++ b/config/default-form-settings.php @@ -10,5 +10,6 @@ 'required_fields' => '', 'update_existing' => 0, 'subscriber_tags' => '', + 'remove_subscriber_tags' => '', 'email_typo_check' => 0, ]; diff --git a/includes/forms/class-form.php b/includes/forms/class-form.php index 275e2b7c..2fee9564 100755 --- a/includes/forms/class-form.php +++ b/includes/forms/class-form.php @@ -773,12 +773,33 @@ public function get_message($key) public function get_subscriber_tags() { $tags = []; - foreach (explode(',', $this->settings['subscriber_tags']) as $v) { + + // Add active tags + $tags = array_merge($tags, $this->parse_tags_from_setting($this->settings['subscriber_tags'], 'active')); + + // Add inactive (remove) tags + $tags = array_merge($tags, $this->parse_tags_from_setting($this->settings['remove_subscriber_tags'], 'inactive')); + + return $tags; + } + + /** + * Parse comma-separated tags from a setting into Mailchimp API format + * + * @since 4.10.10 + * @param string $setting_value + * @param string $status + * @return array + */ + private function parse_tags_from_setting($setting_value, $status) + { + $tags = []; + foreach (explode(',', $setting_value) as $v) { $v = trim($v); if ($v == '') { continue; } - $tags[] = $v; + $tags[] = ['name' => $v, 'status' => $status]; } return $tags; } diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index c6222290..ea3c79e7 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,7 +95,7 @@ - +

@@ -106,6 +106,18 @@ + + + + +

+ + +

+ + + + From d5b6813ea46c9ecc312bbce00ca4f04dc8c56061 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 12:49:48 +0100 Subject: [PATCH 4/9] testing 123 --- tests/FormTest.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/FormTest.php b/tests/FormTest.php index eaa34461..7202371c 100755 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -276,9 +276,17 @@ public function test_get_subscriber_tags() $post = get_post(1); $form = new MC4WP_Form(1, $post); $form->settings = [ - 'subscriber_tags' => 'foo,,bar' + 'subscriber_tags' => 'foo,bar', + 'remove_subscriber_tags' => 'old,,tag' ]; - $this->assertEquals($form->get_subscriber_tags(), ['foo', 'bar']); + $this->assertEquals($form->get_subscriber_tags(), [ + ['name' => 'foo', 'status' => 'active'], + ['name' => 'bar', 'status' => 'active'], + ['name' => 'old', 'status' => 'inactive'], + ['name' => 'tag', 'status' => 'inactive'] + ]); } + + } From 94dca0bf907c1378d108ad3a54a3a00a53b67d37 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 12:52:35 +0100 Subject: [PATCH 5/9] Update FormTest.php --- tests/FormTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/FormTest.php b/tests/FormTest.php index 7202371c..223b10aa 100755 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -287,6 +287,4 @@ public function test_get_subscriber_tags() ['name' => 'tag', 'status' => 'inactive'] ]); } - - } From 4bccf59d24b8150cf79f8949c981a279b8befc74 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 14:18:03 +0100 Subject: [PATCH 6/9] Textual updates for consistency --- includes/forms/views/tabs/form-settings.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index ea3c79e7..4c6f109a 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,7 +95,7 @@ - +

@@ -107,9 +107,9 @@ - + - +

From 94f87700410ff3167b0be54422c18ad7afc66a11 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 14:22:04 +0100 Subject: [PATCH 7/9] textual updates for consistency in form settings view --- includes/forms/views/tabs/form-settings.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index 4c6f109a..e3d94e65 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,9 +95,9 @@ - + - +

@@ -107,9 +107,9 @@ - + - +

From eaa2627c4f524c576496af4b41a399105efd90b5 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Mon, 15 Dec 2025 17:10:27 +0100 Subject: [PATCH 8/9] Updated code snippets to showcase remove tags --- sample-code-snippets/forms/subscriber-tags.php | 18 ++++++++++++++++-- .../contact-form-7/different-tag.php | 8 ++++++++ .../ninja-forms/different-tag-per-form.php | 8 ++++++++ .../woocommerce/woocommerce-subscriber-tag.php | 17 ++++++++++++++++- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/sample-code-snippets/forms/subscriber-tags.php b/sample-code-snippets/forms/subscriber-tags.php index 8c5946cb..b4241998 100755 --- a/sample-code-snippets/forms/subscriber-tags.php +++ b/sample-code-snippets/forms/subscriber-tags.php @@ -1,13 +1,27 @@ tags[] = 'My tag'; return $subscriber; }); + +//This will remove the tag "My tag" from all new subscribers added by the plugin. +add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) { + $subscriber->tags[] = ['name' => 'My Tag', 'status' => 'inactive']; + return $subscriber; +}); + + +//You can add and remove multiple tags at once. +add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) { + $subscriber->tags[] = ['name' => 'My Tag', 'status' => 'active']; + $subscriber->tags[] = ['name' => 'Another tag', 'status' => 'active']; + $subscriber->tags[] = ['name' => 'Remove this tag', 'status' => 'inactive']; + return $subscriber; +}); diff --git a/sample-code-snippets/integrations/contact-form-7/different-tag.php b/sample-code-snippets/integrations/contact-form-7/different-tag.php index 331c6226..a2933df9 100755 --- a/sample-code-snippets/integrations/contact-form-7/different-tag.php +++ b/sample-code-snippets/integrations/contact-form-7/different-tag.php @@ -1,5 +1,13 @@ tags[] = 'ES'; diff --git a/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php b/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php index 54716d65..669b4690 100755 --- a/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php +++ b/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php @@ -1,5 +1,13 @@ tags[] = 'ES'; diff --git a/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php b/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php index c927aa31..74a6f6dc 100755 --- a/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php +++ b/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php @@ -1,11 +1,26 @@ tags[] = 'My tag'; return $subscriber; }); + + +//Remove the tag "My tag" from all new subscribers added using WooCommerce Checkout integration. +add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) { + $subscriber->tags[] = ['name' => 'My tag', 'status' => 'inactive']; + return $subscriber; +}); + +//Add the tag "My tag" to all new subscribers added using WooCommerce Checkout integration while removing the "Remove me" tag. +add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) { + $subscriber->tags[] = ['name' => 'My tag', 'status' => 'active']; + $subscriber->tags[] = ['name' => 'Remove me', 'status' => 'inactive']; + return $subscriber; +}); From 2c338635715d557dcbd249c7ddb6de2465e385ac Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Tue, 16 Dec 2025 11:19:58 +0100 Subject: [PATCH 9/9] Added warning about cf7 ID --- .../integrations/contact-form-7/different-tag.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sample-code-snippets/integrations/contact-form-7/different-tag.php b/sample-code-snippets/integrations/contact-form-7/different-tag.php index a2933df9..bc7b55ab 100755 --- a/sample-code-snippets/integrations/contact-form-7/different-tag.php +++ b/sample-code-snippets/integrations/contact-form-7/different-tag.php @@ -5,6 +5,9 @@ * In this example CF7 form with ID 500 will get the tag ES. * The form ID 510 will get the tag NL * + * NOTE: The ID in this case is the POST ID that you see in the URL when you edit a form. + * That means the ID is always a number and not the ID you see in the CF shortcode. + * * It is also possible to remove tags, see the subscriber-tags.php snippet under /forms/ */