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.
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
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:
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.
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.
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.
Manage Cookie Consent
We use cookies to optimize our website and services. Your consent allows us to process data such as browsing behavior. Not consenting may affect some features.
Functional
Always active
Required for our website to operate and communicate correctly.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
We use these to analyze the statistics of our site. Collected information is completely anonymous.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
These cookies track your browsing to provide ads relevant to you.