Skip Navigation

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

Problem:
The client is experiencing an issue where a custom JavaScript code used for language redirection is not functioning correctly after a WPML update. Instead of redirecting to the correct language path (e.g.,

/fr

), it is adding an additional language code (e.g.,

/en-us/fr

).
Solution:
We understand that debugging custom code is beyond our support policy. However, our second-tier supporters suggest modifying the JavaScript code to ensure proper redirection. Here are the steps to adjust the code:
1. Remove the concatenation of the current path with the modified path.
2. Set the

newURL

variable to only the modified path.
Here is the revised code:

<script><br />function changePath() {<br />    // Get the current hostname<br />    var currentURL = window.location.hostname;<br /><br />    // Extract the current path from the URL<br />    var currentPath = window.location.pathname;<br /><br />    // Modify the path<br />    var modifiedPath = '/fr';<br /><br />    // Set the new URL to the modified path<br />    var newURL = modifiedPath;<br /><br />    // Update the URL<br />    window.location.href = newURL;<br /><br />    // Log it<br />    console.log(newURL)<br />  }<br /></script>

Please note that while we hope this solution is helpful, we do not guarantee that the code will work, nor can we provide support for this custom code.
If this solution does not seem relevant to your issue, please open a new support ticket with us.

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.

This topic contains 4 replies, has 2 voices.

Last updated by Itamar 1 year, 5 months ago.

Assisted by: Itamar.

Author Posts
December 9, 2023 at 4:13 pm #15029623

fredericS-31

I recently updated my website and I have a popup where visitors can select their language/region, one of them is French and it was working correctly until the update. The code on the flag is

 <script>
function changePath() {
    // Get the current hostname
    var currentURL = window.location.hostname;

    // Extract the current path from the URL
    var currentPath = window.location.pathname;

    // Modify the path
    var modifiedPath = '/fr';

    // Concatenate the current path with the modified path
    // You did a replace but your comment speaks of concatenate
    var newURL = modifiedPath + currentPath

    // Update the URL
    window.location.href = newURL;
    
    // Log it
    console.log(newURL)
  }
</script>


What it does now, is that it redirects to a url with en-us preceding, for example, homepage redirects to boyerbrush.ca/en-us/fr

It was working correctly before update, redirecting to boyerbrush.ca/fr

Any help is appreciated

December 11, 2023 at 8:49 pm #15040869

Itamar
WPML Supporter since 02/2016

Languages: English (English )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

I'm sorry, but debugging custom code is out of the scope of our support forum. You can read our support policy here.

https://wpml.org/purchase/support-policy/

If you find something with WPML that is not working as expected, and you can point us to the source of the problem in our code, we will look into it and help. And if needed, we will escalate it to our developers.

Otherwise, if you need further help with what you are trying to achieve, I would suggest you consider contacting one of our certified partners from this link:

https://wpml.org/contractors/

Before doing so, please have a look here:

https://wpml.org/documentation/about-wpml-contractors/what-type-of-work-you-can-expect-from-contractors/

With our contractors, you might get the custom assistance you need to get on with your project.

Regards,
Itamar.

December 12, 2023 at 10:02 am #15043937

fredericS-31

Thanks for your reply Itamar, I'm not asking to debug the code. The problem I'm facing is that before updating WPML, the code would work and now for some reason it is adding the US language (en-us) path to the URL as if that was the default language set in the site, which it isn't.

December 12, 2023 at 10:51 am #15044429

Itamar
WPML Supporter since 02/2016

Languages: English (English )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

For clarification purposes, let me mention the following.

According to the debug information of your site, you have three languages on your site with the following language codes.

Default language: en
Second language: fr
Third language: en-us

en-us must be a custom language you created.

You wrote:

I'm not asking to debug the code. The problem I'm facing is that before updating WPML, the code would work and now for some reason it is adding the US language (en-us) path to the URL as if that was the default language set in the site, which it isn't.

The fact that the code worked in the past and is not working now does not remove the issue from the custom code aspect. It would still require us to debug your custom code. However, I'm sending your code to our second-tier supporters to hear from them if they can give their input on this.

I'll get back to you when I have their reply.

Regards,
Itamar.

December 12, 2023 at 2:22 pm #15046787

Itamar
WPML Supporter since 02/2016

Languages: English (English )

Timezone: Asia/Jerusalem (GMT+03:00)

Hi,

Here is the reply and suggestion from our second-tier supporters.

var newURL = modifiedPath + currentPath

This, if you visit /fr/ and click on en_US - it will be /en-us/fr/

Perhaps just adding the modifiedPath is sufficient:

<script>
function changePath() {
    // Get the current hostname
    var currentURL = window.location.hostname;
 
    // Extract the current path from the URL
    var currentPath = window.location.pathname;
 
    // Modify the path
    var modifiedPath = '/fr';
 
    // Concatenate the current path with the modified path
    // You did a replace but your comment speaks of concatenate
    var newURL = modifiedPath 
 
    // Update the URL
    window.location.href = newURL;
     
    // Log it
    console.log(newURL)
  }
</script>

We hope that this information is helpful to you.

Please know that we do not guarantee the code will work nor we will be able to support this code.

Regards,
Itamar.