In a previous post, I looked at sending Wordpress user information to Mautic using the new plugin options. Around the same time that the user information option was added, a filter was also introduced which allows you to send custom parameters.
In this post, I'm going to show an example of sending a custom attribute to update the preferred locale contact field with information from WPML
You can find information about the filter on the GitHub page.
When a site is made multi-lingual with the WPML plugin, it adds an option to the users profile edit page to choose a preferred locale from your installed languages. This gets stored in the usermeta table in the same format that Mautic uses for the preferred locale which makes for a simple example.
For this method to work for any contact fields, the field needs to be set to be publicly updatable, so first update the preferred_locale field by clicking on the top right gear wheel in the mautic admin, select the custom fields area and then edit the field.
The following snippet can work as a standalone plugin or you can add the code to your themes functions.php file or a custom hooks plugin.
The example code is a standalone plugin. Plugins run too early in the application's lifecycle to know if a user is logged in or not, so the code is wrapped in a function that uses the 'init' action to run at a later date when the user information is available.
Since only Wordpress users can have usermeta and the tracking script is only added on the frontend of the site, we check that the user is logged in and not in the admin area before continuing.
If the checks pass, then we check for a 'locale' field in the usermeta table. This will return false if the value is not set or the default option of 'default site language' is set by the user.
Rather than running the filter when we have no value which could delete information already set in Mautic, the filter is wrapped in a check so we know the value is not false.
Since the usermeta is found outside of the filter, it needs to be declared as a global outside and inside of the filter so that the value is accessible.
If we get this far then all that is left to be done is to add our value to the attrs array with a key that has the same name as the alias of the field that we want to update.
$attrs['preferred_locale'] = $user_locale;
Once the plugin is enabled, you can test that the new variable is being sent by logging into your site and updating your profile to set a preferred language. Then check the network tab of your browsers developer tools to check that the information is being sent correctly (You can filter the requests in Chrome using the domain of your Mautic install).
When using this method, I would advise turning on the Wordpress user tracking in the main Mautic plugin and set the email field to be publicly updateable. As the email field is unique, this prevents the information from being attached to anonymous users when people log in on different devices.
Since a user is generally not going to be changing their preferred language very often, it's overkill to be sending this information on every page hit. If you were to use this on a production site then you may want to add another check so that it only runs on the user profile update page (If you have frontend user profile updating).