Skip Navigation

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

Problem:
When I search in Italian or English, when I click on the language selector, I do not stay on the search page. Instead, I am redirected to a product page.

Solution:
This was not caused by WPML. Custom code was used in this setup, causing a redirect

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 17 replies, has 2 voices.

Last updated by Marcel 1 year, 7 months ago.

Assisted by: Marcel.

Author Posts
November 10, 2023 at 6:29 pm #14779839

Marcel
Supporter

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

Timezone: Europe/Madrid (GMT+02:00)

Hi,

thanks for the video. The redirect doesn't happen to me when I switch to English. Actually, nothing is happening.

I reactivated all plugins except WPML, WooCommerce, WooCommerce Multilingual and the Avada Plugins, then switching the language works, but still here without redirect: hidden link.

This comes from a 301 redirect on your installation:

>>> <em><u>hidden link</u></em>

> --------------------------------------------
> 301 Moved Permanently
> --------------------------------------------

Status:	301 Moved Permanently
Code:	301
Date:	Fri, 10 Nov 2023 18:26:45 GMT
Server:	Apache
X-Redirect-By:	WordPress
Upgrade:	h2,h2c
Connection:	Upgrade, close
Location:	<em><u>hidden link</u></em>
Content-Length:	0
Content-Type:	text/html; charset=UTF-8

>>> <em><u>hidden link</u></em>

> --------------------------------------------
> 200 OK
> --------------------------------------------

Best Regards
Marcel

November 12, 2023 at 12:25 pm #14783855

fabioC-16

I found the problem, at least for my setup.

That page ID referred to the "shop page", a WooCommerce page.

I deleted the page and dynamically changed the "href" value of the link in the language selector, adding a JS function.

The point is that using plugins (e.g. FiboSearch) to allow searching by SKU and only showing products as search results creates some problems with the language selector.

The JS code, added via function.php is this:

function add_custom_script() {
?>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
// Check if querystring 's' is present
var queryString = window.location.search;
var urlParams = new URLSearchParams(queryString);
if (!urlParams.has('s')) {
return; // If 's' is not present, stop the script
}

// Function to construct the URL based on the language
function constructUrlLanguage(baseUrl, language) {
var url = new URL(baseUrl);
var pathname = url.pathname;

// Adjust the path based on the language
if (language === 'it') {
pathname = pathname.replace('/en/', '/');
} else if (language === 'en') {
if (!pathname.includes('/en/')) {
pathname = '/en' + pathname;
}
}
url.pathname = pathname;

// Update the 'lang' parameter
url.searchParams.set('lang', language);

return url.href;
}

// Update the href for Italian
var linkIt = document.querySelector('#menu-item-wpml-ls-39-it a');
if (linkIt) {
linkIt.href = buildUrlLanguage(window.location.href, 'it');
}

// Update the href for English
var linkEn = document.querySelector('#menu-item-wpml-ls-39-en a');
if (linkEn) {
linkEn.href = buildLanguageUrl(window.location.href, 'en');
}
});
</script>
<?php
}
add_action('wp_footer', 'add_custom_script');

If anyone was in the same situation as me and if they needed it.

Thanks for the support, Marcel.

November 13, 2023 at 8:20 am #14786071

Marcel
Supporter

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

Timezone: Europe/Madrid (GMT+02:00)

Hi,

thanks for the info about your custom coding. I will mark this as resolved then.

Best Regards
Marcel