Skip to content

Instantly share code, notes, and snippets.

@robwent
Last active February 22, 2020 18:41
Show Gist options
  • Save robwent/1278db5b00b1a08fe2ab73e0d70e5b87 to your computer and use it in GitHub Desktop.
Save robwent/1278db5b00b1a08fe2ab73e0d70e5b87 to your computer and use it in GitHub Desktop.
Send WPML preferred language to Mautic
<?php
/*
Plugin Name: Mautic WPML Preferred Language
Plugin URI: https://www.robertwent.com/
Description: Sends WPML language preference to Mautic
Version: 1.0
Author: Robert Went
*/
function mautic_wpml_lang_init() {
// Only run for known users on the frontend
if (is_user_logged_in() && !is_admin()) {
// The var needs to be global to be able to get the value in the filter
global $user_locale;
// Check if the user has set a preferred locale
$current_user = wp_get_current_user();
$user_locale = get_user_meta( $current_user->ID, 'locale', true );
// Only call the filter if there is a value set
if ($user_locale) {
add_filter('wpmautic_tracking_attributes', function($attrs) {
// Access the variable set outside the function
global $user_locale;
// Attach the lang to the attributes array
$attrs['preferred_locale'] = $user_locale;
// Return the new array to the main plugin
return $attrs;
});
}
}
}
// We use the init action as plugins run too early by default to check if a user is logged in
add_action('init', 'mautic_wpml_lang_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment