Tag Every Mailchimp Subscriber From Your Site
Adds tags such as "website" and the visitor's language to every subscriber Mailchimp for WordPress sends to Mailchimp, from forms and integrations alike, so you can segment by source.
Opens WPIDE on
Code
$wpide_tags = array( 'website' ); // Tags added to every subscriber.
add_filter( 'mc4wp_subscriber_data', function ( $subscriber ) use ( $wpide_tags ) {
if ( ! is_object( $subscriber ) ) {
return $subscriber;
}
$lang = strtok( determine_locale(), '_' );
if ( function_exists( 'pll_current_language' ) && pll_current_language() ) {
$lang = pll_current_language();
} elseif ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE ) {
$lang = ICL_LANGUAGE_CODE;
}
$tags = $wpide_tags;
$tags[] = 'lang-' . sanitize_key( $lang );
$subscriber->tags = array_values( array_unique( array_merge( isset( $subscriber->tags ) ? (array) $subscriber->tags : array(), $tags ) ) );
return $subscriber;
} );
Notes
Edit $wpide_tags. The language tag uses the site or WPML/Polylang language.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.