Skip Navigation

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 WPMLLanguages, 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 ); 

8 Responses to “YOAST SEO - Canonical link is incorrect on paginated pages in secondary language when using language as a parameter”

  1. Hi,
    I have encountered this problem discovered when analysing the head tag

    N’est pas une URL absolue (https%3A%2F%2Fscience-sense.com%2F)

    Finally I corrected the problem by dis-activating your plugin part of the wpml bundle.

    I tried your code (activated) in the functions file and cleared the site and navigator cache but the problem persisted.

    Please tell me what to do next so that I can use this paid version of wpml SEO

    Regards

  2. Hi,

    I’ve added this code and it seemed to work but now I get the following fatal error:

    Fatal error: Uncaught Error: call_user_func_array(): Argument #1 ($callback) must be a valid callback, function “wpmldp_filter_wpseo_canonical_lang_parameter_position” not found or invalid function name
    in /home/xxx.cloudwaysapps.com/nvvxjzdyea/public_html/wp-includes/class-wp-hook.php on line 310

    Oproepstack:

    WP_Hook::apply_filters()
    wp-includes/plugin.php:205
    apply_filters()
    wp-content/plugins/wordpress-seo/src/presenters/canonical-presenter.php:50
    Yoast\W\S\P\Canonical_Presenter::get()
    wp-content/plugins/wordpress-seo/src/presenters/abstract-indexable-tag-presenter.php:37
    Yoast\W\S\P\Abstract_Indexable_Tag_Presenter::present()
    wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:326
    Yoast\W\S\I\Front_End_Integration::present_head()
    wp-includes/class-wp-hook.php:308
    WP_Hook::apply_filters()
    wp-includes/class-wp-hook.php:332
    WP_Hook::do_action()
    wp-includes/plugin.php:517
    do_action()
    wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:300
    Yoast\W\S\I\Front_End_Integration::call_wpseo_head()
    wp-includes/class-wp-hook.php:308
    WP_Hook::apply_filters()
    wp-includes/class-wp-hook.php:332
    WP_Hook::do_action()
    wp-includes/plugin.php:517
    do_action()
    wp-includes/general-template.php:3043
    wp_head()
    wp-content/themes/flatsome/header.php:17
    require_once()
    wp-includes/template.php:783
    load_template()
    wp-includes/template.php:718
    locate_template()
    wp-includes/general-template.php:48
    get_header()
    wp-content/themes/flatsome/page-blank.php:9
    include()
    wp-includes/template-loader.php:106
    require_once()
    wp-blog-header.php:19
    require()
    index.php:17

    Is there another way to temporarily fix this issue?

    Cheers,
    Jaap

    • Hello Jaap,
      I’ve updated this page as the workaround lost its format and there could have an issue. Could you please give it another try?
      If it still does not work, please open a chat in our support forum and share your findings.
      We will investigate it further.
      Regards

  3. How about if pagination is with query strings in the format of /?page=2

    then what should we do if we want yoast seo to generate correct urls in canonical and rel next prev seo tags?

    • Hey there,
      This issue was related to Yoast plugin and not WPML. It should have been solved with Yoast SEO 20.3 version. I recommend you to contact their support for further information or if you are still experiencing this issue.
      Regards

Leave a Reply

Please stay on topic and be respectful to others. If you need help with issues not related to this post, use our Support Forum to start a chat or submit a ticket.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>