Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client needed to set up user locale (language) via an API for a WordPress site using WPML. They wanted to update the user's language preference, which is collected in an external system, to be used by a membership plugin for sending localized messages. The 'locale' field in the wp_usermeta table was not being updated when the language was switched using the language switcher, only when changed in the admin panel.

Solution:
We informed the client that the 'locale' field in the wp_usermeta table is not specific to WPML and is present even in a WordPress installation without WPML. This field is empty by default and only gets a value when the language is changed from 'Site default' to a specific language inside the user's WordPress profile.

To change the 'locale' value programmatically, we suggested looking at a Stack Overflow thread: https://stackoverflow.com/questions/8236280/setting-wordpress-language-programmatically.

We also clarified that WPML does not provide a hook to change the user language directly, but we do offer hooks that could be useful:

If this solution is not relevant due to being outdated or not applicable to your case, we recommend opening a new support ticket. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins.

For further assistance, please contact us through the WPML support forum: https://wpml.org/forums/forum/english-support/.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Andreas W. 1 year, 2 months ago.

Assisted by: Andreas W..

Author Posts
March 8, 2024 at 12:39 pm #15389287

dominikC-4

Hi,
We need to be able to set up user locale (language) via an API. Is there a WPML endpoint that would allow us to do that. We collect the users language information mainly in an external system but later the information about users language would be used by our membership plugin (Paid Membership Pro) to send localized membership related messages. What's the optimal way to do that with WPML?

March 8, 2024 at 6:28 pm #15390455

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

You would not need a WPML hook for this.

The user language in WordPress is saved inside the wp_usermeta table inside the option called "locale".

Further, our available Actions and Filters can be found here:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

Best regards
Andreas

March 9, 2024 at 6:43 am #15391105

dominikC-4

But the values are empty in locale records in wp_usermeta. Even when user switches the language with the switcher. Only when the language is changed in the admin panel in the user profile then the language is saved to locale.
We don't give access to WordPress admin pages. We need set it via REST API call. Our users declare their language in pur app which is external to WordPress. Once they do this we need to call WordPress endpoint and update locale value. Does WPML have REST API endpoint for that?

March 9, 2024 at 12:09 pm #15391377

Andreas W.
WPML Supporter since 12/2018

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Yes, this "locale" option on the usermeta table is by default empty but it is not a WPML Specific field. It can be found even on a WordPress install without WPML.

Once you go to a user profile on wp-admin and change the language setting from "Site default" to a specific language, then the "locale" option will have a value.

Means, "Site default" is here the WordPress default setting and this is why "locale" by default is empty.

More details:
https://wpml.org/documentation/getting-started-guide/language-setup/setting-the-wordpress-administration-language/

If a user registers on Frontend, then the "locale" option will also not be set, as it only takes a value from wp-admin and, not from the user language on Frontend.

In order to change this value programmatically maybe this thread will be useful for you:
https://stackoverflow.com/questions/8236280/setting-wordpress-language-programmatically

WPML does not come with a hook that would allow to change the user language:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#

But we do have a hook that allows you to switch the language for emails, if necessary:
https://wpml.org/wpml-hook/wpml_switch_language_for_email/

We do also have a hook that allows you to set condtions and functions for the currently active language, which would also work for users on Frontend:
https://wpml.org/wpml-hook/wpml_current_language/