Hi @fdetourneint,
Could you check if “WPML/Polylang language mismatch” option is turned on in Permalink Manager settings?
Best regards,
Maciej
Hi Maciej,
I had seen this on a Polylang forum but for WPML I couldn’t find it. In fact it was the same setting. It’s working fine now!!
Thank you very much !
FD
Hi Maciej,
I have another problem when I turn on “WPML/Polylang language mismatch” option.
For example, I have a page with different language :
– in FR : domain.com/la-region
– in EN : domain.com/en/the-region
– in NL : domain.com/nl/de-region
When WMPL option turn on, if I test this url : domain.com/en/la-region, I have 304 http code with html in english but i have no redirection to domain.com/en/the-region.
How can I have a redirection 301 to domain.com/en/the-region for this example ?
Thanks,
FD
Hi @fdetourneint,
Unfortunately this is not possible with the plugin itself at this moment. However, you can try to use the below code snippet to check the language mismatch:
function pm_fix_language_mismatch($item_id, $uri_parts, $is_term = false) {
if(!$is_term) {
$element = get_post($item_id);
if(!empty($element->post_type)) {
$element_id = $item_id;
$element_type = $element->post_type;
}
}
// Stop if no term or post is detected
if(empty($element)) { return false; }
// Get the language code of the found post/term
$element_language_code = Permalink_Manager_Language_Plugins::get_language_code($element);
// Get the detected language code
if(defined('ICL_LANGUAGE_CODE')) {
$detected_language_code = ICL_LANGUAGE_CODE;
} else if(!empty($uri_parts['lang'])) {
$detected_language_code = $uri_parts['lang'];
} else {
return $item_id;
}
if($detected_language_code !== $element_language_code) {
$item_id = apply_filters('wpml_object_id', $element_id, $element_type);
if(!empty($item_id)) {
$canonical_permalink = get_permalink($item_id);
wp_safe_redirect($canonical_permalink, 301);
exit();
}
}
return $item_id;
}
add_filter('permalink_manager_detected_post_id', 'pm_fix_language_mismatch', 7, 3);
Hi Maciej,
Thank you for your code, it works.
FD