Home›Support›English Support›[Resolved] Customizing an expiration date of wp-wpml_current_language cookie
[Resolved] Customizing an expiration date of wp-wpml_current_language cookie
This thread is resolved. Here is a description of the problem and solution.
Problem: The client is trying to write custom logic to redirect users based on browser language and wants to extend the expiration of the "wp-wpml_current_language" cookie from a session to 2 years to honor user's language preferences. Additionally, the client seeks a way to detect when a user changes the language using the WPML language selector without enabling the WPML Browser Language redirect.
Solution: We recommend using the wpml_browser_redirect_language_params hook to customize browser language redirection behavior. However, we advise against changing the cookie value as it may cause issues with the translated content.
WPML stores cookies that are essential for its proper functioning, as detailed in our documentation on browser cookies stored by WPML.
For detecting language switches, the wpml_language_has_switched hook can be used to perform actions when the global language changes in WPML.
Please note that any custom work beyond WPML's settings is not covered by our support policy, which you can review here. If you require further customization, you may consider contacting WPML-experienced developers at WPML contractors.
If this solution does not apply to your case, or if it seems outdated, we highly recommend checking related known issues at WPML known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. Should you need further assistance, please do not hesitate to open a new support ticket in our support forum.
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.
I am trying to write custom logic that redirects users based on browser language. I want to check the value of the cookie "wp-wpml_current_language" during the check. I see this cookie has an expiration of "Session". I'm wondering if this can be customized to something like 2 years? I am trying to honor user's language preference for longer than just a session.
If this is not possible, I'm wondering if there is a way to prevent the "wp-wpml_current_language" from being set on the first visit?
Is there any documentation that you are following?
This is slightly helpful. Is there a way to know if a user has changed a language using the WPML language selector without needing to have the WPML Browser Language redirect enabled? For example, if I visit the site in English (default), and I want to change the site to French (fr), the value of the wp-wpml_current_language cookie is changed to "fr".
The issue I am having is if you enter a new session in a different language, it sets the value of wp-wpml_current_language to that language code rather than sticking to the last set language by the user.
For example, if I enter the site with /pt-br/ in the URL, the value of wp-wpml_current_language automatically updates to "pt-br". If I change it to French, it changes to "fr". When I close the session, everything resets because the expiration of wp-wpml_current_language is session only.
I want to essentially listen for a language switch on the backend using PHP so that I can set a different cookie value to match that of the wp-wpml_current_language cookie.
JavaScript redirect won't work because it's slow and creates a weird user experience. Therefore, we are building our own.
The missing piece is being able to know when the cookie value changes based on when the language is changed. If I can store the last known set language by the user, then I can reference this in future entries to the site by that user.
Languages: English (English )Spanish (Español )German (Deutsch )
Timezone: America/Lima (GMT-05:00)
Take kindly note, that I can not suggest changing the cookie value, as it might cause issues with the translated content, especially when running post or search queries.
"WPML only stores cookies that are important for it to function properly."
Short:
It is expected in this case that the currently active language code is equal to wp-wpml_current_language and this is how WPML needs it to work as expected.
Also, when it comes to redirecting by browser language the WPML approach is the only option we can offer and we can not support anything that goes beyond the settings that WPML is offering.
We usually use this filter conditionally to force something to happen if a specific language is the actual current language.
It is very likely that also this hook somehow depends on the cookie value, but this is more an internal development topic and not a question that our supporters are supposed to know the exact answer to.