it still redirects me to home page on some instances - usually, works well, but feels buggy. Let's say im on About Us page, sometimes it redirects me to home page, sometimes changes the language of current page perfectly
When I navigate between pages and hover the lang switcher a element, sometimes it shows the correct URL it should direct me on lang change, sometimes its just landing page / home page.
Thank you for contacting WPML Support, I will gladly help you.
Please note that custom coding is not covered by our Support Policy. The code provided might not work on your website, and in case you feel that you need more help with this, I recommend that you hire a developer or a WPML contractor: https://wpml.org/contractors.
Here is an updated version of your function with added debugging to help track down the issue:
1) Current URL Calculation: It dynamically calculates the current URL and appends the appropriate language parameter. This should help in maintaining the context of the current page while switching languages.
2) Debug Information: You might want to log or examine $current_url and the final URLs ($lang_url) to see if they're being constructed as expected.
* Given that the issue is somehow random I would suggest checking the cache of the website as well.
I suggest you test the code provided in a staging environment to avoid any possible issues that might be caused by it.
I hope this information is helpful and thanks for understanding.
Thanks for the reply.
Unfortunately this did not solve for the issue.
It only added '?lang=x' query to URL, which.. is not right for the setup.
URL structure is domain.xx/en or domain.xx/otherlang/. So it uses language folders.
The website indeed has caching implemented, but... this seems more like the issue of WPML not custom coding - if it works, but inconsistently, i feel like issue must be somewhere else...