Understanding and Customizing Polylang Hreflang Tags for Better SEO
Content
If you're using the Polylang plugin for a multilingual WordPress site, you've likely encountered hreflang tags. These HTML attributes in your page's <head> section are crucial for SEO, as they tell search engines like Google about the language and regional targeting of your content. A common point of confusion among users is how to control and customize these automatically generated tags.
What Are Hreflang Tags and Why Are They Important?
Polylang automatically generates <link rel="alternate" hreflang="xx" /> tags for all translated versions of a post or page. This helps search engines understand the relationship between different language versions, preventing issues with duplicate content and ensuring users are served the correct language version in search results.
Common Hreflang Challenges and Solutions
Based on community discussions, here are the most frequent issues and how to address them.
1. Adding an x-default Hreflang Tag
An x-default hreflang tag specifies a default page for users whose language doesn't match any of the provided alternatives. While Polylang adds this automatically in some cases (like when the homepage auto-redirects), you may need to add it manually. This can be done by adding a custom function to your theme's functions.php file.
function polylang_add_xdefault( $hreflangs ) {
$default = array(
'x-default' => reset( $hreflangs ) // Uses the first language's URL
);
return $hreflangs + $default; // Appends x-default to the end of the array
}
add_filter( 'pll_rel_hreflang_attributes', 'polylang_add_xdefault', 10, 1 );
To place the x-default tag at the beginning of the list instead, change the + operator to merge the arrays to $default + $hreflangs.
2. Changing Language Codes to Include Regions
By default, Polylang may use a simple language code (e.g., en). For better regional targeting, you might want to use a language-region code (e.g., en-GB or ru-UA). You can modify these codes using a filter.
add_filter( 'pll_rel_hreflang_attributes', function( $hreflangs ) {
// Replace 'ru' with 'ru-UA'
if (isset($hreflangs['ru'])) {
$hreflangs['ru-UA'] = $hreflangs['ru'];
unset( $hreflangs['ru'] );
}
// Replace 'en' with 'en-GB'
if (isset($hreflangs['en'])) {
$hreflangs['en-GB'] = $hreflangs['en'];
unset( $hreflangs['en'] );
}
return $hreflangs;
} );
3. Removing the Self-Referencing Hreflang Tag
Some users notice a tag pointing to the page itself (e.g., hreflang="fr" on a French page) and wish to remove it. It's important to note that Google recommends including this self-referencing tag. However, if you choose to remove it, you can filter the array to unset the entry for the current language.
add_filter( 'pll_rel_hreflang_attributes', function( $hreflangs ) {
// Get the current language
$current_lang = pll_current_language('slug');
// Remove the hreflang tag for the current language
if (isset($hreflangs[$current_lang])) {
unset( $hreflangs[$current_lang] );
}
return $hreflangs;
} );
Important Considerations
- Cache: After adding these code snippets to your
functions.phpfile, clear your site's cache (including any caching plugins) to see the changes. - Child Theme: Always use a child theme when modifying theme files to prevent your changes from being overwritten during updates.
- Testing: Use tools like Google's Search Console or third-party hreflang validators to check your implementation after making changes.
By understanding how Polylang's pll_rel_hreflang_attributes filter works, you can gain fine-grained control over your multilingual SEO signals. For more detailed documentation, you can refer to the official Polylang filter reference.
Related Support Threads Support
-
redirect to another sitehttps://wordpress.org/support/topic/redirect-to-another-site/
-
Language code doesn’t changehttps://wordpress.org/support/topic/language-code-doesnt-change/
-
How to remove hreflang and lang attributes from language switcherhttps://wordpress.org/support/topic/how-to-remove-hreflang-and-lang-attributes-from-language-switcher/
-
x-default hreflang last element of attributes listhttps://wordpress.org/support/topic/x-default-hreflang-last-element-of-attributes-list/
-
Autoredirect recomendation Bannerhttps://wordpress.org/support/topic/autoredirect-recomendation-banner/
-
tag page on front end missing hreflang with alternate linkhttps://wordpress.org/support/topic/tag-page-on-front-end-missing-hreflang-with-alternate-link/
-
alternate herflang tag on every language versions of sitehttps://wordpress.org/support/topic/alternate-herflang-tag-on-every-language-versions-of-site/
-
How to remove the rel=”alternate” mapping itself?https://wordpress.org/support/topic/how-to-remove-the-relalternate-mapping-itself/
-
Hreflang problemhttps://wordpress.org/support/topic/hreflang-problem-4/
-
HTML lang using locale setting instead of language codehttps://wordpress.org/support/topic/html-lang-using-locale-setting-instead-of-language-code/
-
Individual Language Switch Links without Using the Menuhttps://wordpress.org/support/topic/individual-language-switch-links-without-using-the-menu/
-
Switch language without lang codehttps://wordpress.org/support/topic/switch-language-without-lang-code/
-
language switch flaghttps://wordpress.org/support/topic/language-switch-flag/
-
Question about US and UK Englishhttps://wordpress.org/support/topic/question-about-us-and-uk-english/
-
Content-Security-Policy issuehttps://wordpress.org/support/topic/content-security-policy-issue-2/
-
Internal Links to other languagehttps://wordpress.org/support/topic/internal-links-to-other-language/
-
Wrong AMPHTML in Multilangual Websitehttps://wordpress.org/support/topic/wrong-amphtml-in-multilangual-website/
-
Hreflang conflicts within page source codehttps://wordpress.org/support/topic/hreflang-conflicts-within-page-source-code-2/
-
” hreflang=”ru-UA”https://wordpress.org/support/topic/hreflangru-ua/
-
Find equivalent page on another language with Javascripthttps://wordpress.org/support/topic/find-equivalent-page-on-another-language-with-javascript/
-
HTML Lang tag not changinghttps://wordpress.org/support/topic/html-lang-tag-not-changing/
-
Removing hreflang from language switchhttps://wordpress.org/support/topic/removing-hreflang-from-language-switch/