[Resolved] WPML/Browser Language Redirect (JS/PHP)
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.
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:
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.
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.
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.