Send the Visitor's Language to Mailchimp (MC4WP)
Sets Mailchimp's built-in subscriber language for every signup from your site, using the current WordPress, WPML or Polylang language, so Mailchimp can send the right translation of your campaigns.
Opens WPIDE on
Code
add_filter( 'mc4wp_subscriber_data', function ( $subscriber ) {
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;
}
$subscriber->language = strtolower( substr( sanitize_key( $lang ), 0, 2 ) );
return $subscriber;
} );
Notes
Mailchimp expects codes like en, fr, de or pt_PT; regional variants are mapped to the two-letter code.
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.