- Status
- Resolved by author
- Reported for
- WPML SEO 2.0.1
Overview of the issue
This is a general problem with Yoast SEO (not specific to WPML) and it has been reported here.
If you are formatting your URLs to display language as a parameter in WPML → Languages, you will notice that pages in secondary languages that are paginated will display an incorrect canonical link.
Workaround
Please, make a full backup of your site before proceeding. Then, add the following code in your functions.php theme file:
// define the wpseo_canonical callback
function wpmldp_filter_wpseo_canonical_lang_parameter_position( $canonical ) {
// decode URL
$canonical = urldecode($canonical);
// get current language
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
// define ?lang parameter
$param = '?lang='.$my_current_lang;
// check if current $canonical contains the parameter
if(strpos($canonical, $param) !== false){
// remove the parameter from URL
$canonical = str_replace('/'.$param, "", $canonical);
// add it again
$canonical = $canonical.$param;
}
return $canonical;
};
// add the filter
add_filter( 'wpseo_canonical', 'wpmldp_filter_wpseo_canonical_lang_parameter_position', 999 );
