Skip Navigation

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

Problem:
The client is trying to implement a browser language redirect on their homepage, using JavaScript, to guide users to their preferred language version of the site based on browser settings. They want to set a cookie when a user selects a language manually, so that subsequent visits will redirect them to their chosen language homepage. However, they are encountering issues with their solution, which may be conflicting with WPML's own redirection mechanisms.

Solution:
We recommend using custom JavaScript to handle browser redirection and interacting with WPML's features through various hooks:
1. For custom JavaScript handling, use the wpml_enqueue_browser_redirect_language hook.
2. To execute code at the end of WPML's browser redirection logic, use the wpml_enqueued_browser_redirect_language hook.
3. To change the cookie name or modify the language URLs, use the wpml_browser_redirect_language_params hook.
4. Explore more hooks on the WPML Hooks Reference page.

Please be aware that custom code solutions are beyond our support policy, and we cannot take responsibility for any potential impact on site performance or Google Index Ranking. If you need further assistance with custom coding, consider hiring a contractor from our list: WPML Contractors.

Additionally, WPML's Redirect By Browser Language feature only redirects users once and can redirect to the translated homepage if a translation is missing. More information on this feature can be found here: Automatic Redirect Based on Browser Language.

For geotargeting alternatives, you might want to consider these plugins:

For more details about WPML browser language cookies, please refer to: Browser Cookies Stored by WPML.

If this solution does not seem relevant to your issue, please open a new support ticket with us for further assistance: WPML 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.

Tagged: 

This topic contains 1 reply, has 2 voices.

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

Assisted by: Andreas W..

Author Posts
November 7, 2023 at 10:30 am #14745913

lukasP-26

Tell us what you are trying to do?
Browser language redirects, but because of the potential risk only on the homepage. What I'm working on currently is a JS client side solution, but I struggling a little bit. What I'm trying to do is:

Redirect the user based on the browser language if the language is supported by the website (current translations). If not, do not redirect the user. At t he same time, when the user switches the language via the flag/language drop down, then set a cookie for that languague. When the cookie for "German" is set for example, the next time the user visits the website (website.com), he should be redirected (website.com/de), but only on the homepage (for saftey reasonns when it comes to indexing).

Why I want to do that? Because many of our German users (our main users) are somehow visiting the websites default english language. This happens probably because of our marketing, where we promote our website "cargoboard.com". When the user types in the website in the browser he then lands on the english version. Even with the language drop down in the menu, some users seem not the be able to select the right language.

What I was able to do:
Redirect the user based on the browser languauge on the homepage. But that also means, the user is not able to change the language to english on the homepage. So I need cookies (or local storage). So with the cookie set by default when visiting the website based on their browser language, the user will only able to visit the English version, when he clicks on the German flag in the menu (click event = update cookie). Then the user will be redirected on the homepage, when he types in the URL in the browser, which is OK for us.

What isn't working correctly:
My solution does not always work and won't work. In theory it works, but practially not. I assume it's because of WPML own redirects. My idea was to override the WPML cookie aswell with JS.

So. While I'm working on my solution I was hoping you could maybe step in with an solution for me.

---

Is there any documentation that you are following?
Not really, there is no documentation for that. The language redirect for all pages is too risky for us, because we are fighting for rankings and are depended on our website traffic.

---

Is there a similar example that we can see?
There sare some websites who have similar solutions.

---

What is the link to your site?
- hidden link (production)
- hidden link (dev, yes it is not a subdomain)

---

Thank you very much in advance. You can reply in English or German.

Kind regards,
Max

November 7, 2023 at 2:51 pm #14748639

Andreas W.
Supporter

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

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

Hello Max,

What you are asking for was some time ago answered by one of our second-tier support members here:
https://wpml.org/forums/topic/automatic-redirect-to-browser-language-but-only-at-site/

Take note, that we do have a few more hooks that allow you to interact with the feature:

Use a custom JavaScript for handling the browser redirection:
https://wpml.org/wpml-hook/wpml_enqueue_browser_redirect_language/

Executed at the end of the browser redirection PHP logic:
https://wpml.org/wpml-hook/wpml_enqueued_browser_redirect_language/

Different cookie name or tweak the languageUrls array to use different URLs
https://wpml.org/wpml-hook/wpml_browser_redirect_language_params/

More Hooks:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

I do fully understand your concerns and this redirection is an experimental feature for which we do not know how far it could have a bad impact on site performance and Google Index Ranking.

Please take note that providing a custom code solution in such cases is out of the scope of our support team and we can also not take responsibility for such.

WPML Support Policy:
https://wpml.org/purchase/support-policy/

You could instead consult one of our contractors on this matter:
https://wpml.org/contractors/

Also, take note, that WPML Redirect By Browser Language only redirects once.
It further offers to redirect to the translated homepage if a translation does not exist:
https://wpml.org/documentation/getting-started-guide/language-setup/automatic-redirect-based-on-browser-language/

Maybe one of these plugins might be an alternative for you.
https://wpml.org/plugin-functionality/geotargeting/

More details about the WPML browser language cookies can be found here:
https://wpml.org/documentation/support/browser-cookies-stored-wpml/

Best regards
Andreas

November 7, 2023 at 6:49 pm #14751101

lukasP-26

Hey thank you for reply and the provided links. I hadn't time to look into them now, but I think the assistance you can offer regarding my question determines the status of my ticket. Therefore I will mark it as resolved.