Support » Fixing WordPress » Polylang hreflang

  • Sitechecker give my site the following advise:

    I can see that in HTML lang you specify “es-ES”, but in hreflang you only specify “es”.Make sure to match them identically and the issue will be resolved.

    How can I fix this? I am using Polylang and my site does not need locale version. Our public are italian and spanish speaker around the world.
    Thanks,
    Yahaira

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello.

    I can see Polylang sets the HTML lang to es-ES, but sets the hreflang attribute to es. I believe this is because you’re not using different locales, but I understand you do not need this setup.

    According to Google’s recommendations, for the hreflang, it is better to use the language code on its own, without the country code. However, if you want to override this, Polylang has a filter called pll_rel_hreflang_attributes.

    The Polylang documentation for pll_rel_hreflang_attributes is here: https://polylang.pro/doc/filter-reference/#pll_rel_hreflang_attributes

    Here’s a snippet you can add to your theme’s functions.php file to apply a filter for the Spanish and Italian hreflang attributes:

    // Polylang hreflang filter
    function filter_pll_rel_hreflang_attributes( $hreflangs ) {
    	foreach ( $hreflangs as $lang => $url ) {
    		if ( $lang === 'es' ) {
    			printf( '<link rel="alternate" href="%s" hreflang="%s" />' . "\n", esc_url( $url ), esc_attr( 'es-ES' ) );
    		}
    		if ( $lang === 'it' ) {
    			printf( '<link rel="alternate" href="%s" hreflang="%s" />' . "\n", esc_url( $url ), esc_attr( 'it-IT' ) );
    		}
    	}
        return $hreflangs; 
    };
    add_filter( 'pll_rel_hreflang_attributes', 'filter_pll_rel_hreflang_attributes', 10, 1 );

    This should give you something similar to this:

    <link rel="alternate" href="https://example.com/" hreflang="it-IT" />
    <link rel="alternate" href="https://example.com/es/" hreflang="es-ES" />
    <link rel="alternate" href="https://example.com/" hreflang="it" />
    <link rel="alternate" href="https://example.com/es/" hreflang="es" />

    Hope this helps!

    Thread Starter bebelino

    (@bebelino)

    Thanks a lot! I will try with this and let you know.

    Thread Starter bebelino

    (@bebelino)

    Hello, it seems that we would need further help. My developer says it did not work.Can i have your mail? thanks

    Thread Starter bebelino

    (@bebelino)

    I would like you to fix it.

    Hi again. I’m sorry to hear you couldn’t get this to work. Unfortunately, I can’t fix this for you, I can only offer advice via these forums.

    Please try https://jobs.wordpress.net/ or https://jetpack.pro/ and do not accept any hire offers posted to these forums.

    This topic will be closed as per the Forum Rules.

    Thread Starter bebelino

    (@bebelino)

    ok, thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Polylang hreflang’ is closed to new replies.