Герб Укріїни
Believe in Ukraine and the Armed Forces. Glory to Ukraine!

WP Polylang how to change hreflang

Article updated in November 2026.

If your WordPress website uses the Polylang plugin to manage multilingual content, it’s important to set up hreflang tags correctly for each language version of your pages. Hreflang is a link attribute that helps search engines recognize alternative language versions of a page and show the right one to users based on their language and region. This is particularly important for international websites where content is available in several languages.

By default Polylang prints hreflang tags in the page <head> using the language codes you set for each language. Sometimes you want different values, for example en-US instead of en, or uk-UA instead of uk. Polylang provides the pll_rel_hreflang_attributes filter for this (available since Polylang 2.1, and still documented in the official Polylang filter reference).

How to set up hreflang correctly using Polylang filters

  1. Install and configure Polylang: Make sure the plugin is installed, your languages are added, and the pages, posts and categories are linked as translations of each other. Hreflang tags only appear for content that has translations.
  2. Open the functions.php file of your theme: It’s located in your active theme’s folder, wp-content/themes/your-theme/. If you use a third-party theme, put the code into a child theme (or a small custom plugin) so that a theme update does not erase it.
  3. Add the filter for hreflang: The filter receives an array where the keys are language codes and the values are URLs. The code below replaces the default codes with the ones you want. This example is for Ukrainian, Russian, English and Polish:
  4. Save the file: Save the changes in functions.php (and make a backup first, because a syntax error in this file can break the whole site).
  5. Check the result: Open your website, press CTRL+U to view the page source and find the alternate links. With the mapping above (use ru-RU instead of ru-UA if you want that variant), the output looks like this:
Читайте також:  Default page sorting by date in WordPress

Make sure the codes in the filter and in the expected output match. In the original example the Russian code was set to ru-UA in the code but shown as ru-RU in the output, so choose one value and use it consistently.

A shorter version with a mapping array

If you have many languages, a map is easier to maintain than a chain of if blocks:

Читайте також:  Default page sorting by date in WordPress

This version also keeps any other keys untouched, such as x-default, if Polylang or your own code adds one.

Rules for hreflang values

  • Use a language code (ISO 639-1) first, optionally followed by a region code (ISO 3166-1 Alpha 2), for example en or en-GB. A region code on its own is not valid.
  • The region part is optional. If you serve one page for all English speakers, plain en is fine.
  • Every language version should link to all the others and to itself, and the links must be reciprocal.
  • Use absolute URLs, and make sure they match the canonical URLs of the pages.
  • Optionally add x-default for a fallback page for users whose language is not covered. Polylang’s documentation shows how to add it with the same filter.

Common problems

  • Duplicate hreflang tags: An SEO plugin or your theme may also print hreflang tags. Check the source: each language should appear only once per page.
  • Changes not visible: Clear the page cache, the caching plugin and the CDN cache, then reload the page.
  • No tags on a page: The page probably has no linked translation in Polylang.
Читайте також:  Default page sorting by date in WordPress

After the check, you can also look at the international targeting and indexing reports in Google Search Console to make sure that the pages are indexed and that no alternate-page errors appear.

Using an AI assistant for this task

An AI assistant such as Claude can generate the mapping code for your exact list of languages and regions, or review your functions.php snippet for syntax errors before you save it. You can also paste the alternate links from your page source and ask it to check for missing return links or invalid codes. Always test the result on a staging copy of the site first.

That’s it! With correct hreflang tags, search engines can recognize the alternative language versions of your content, which helps users from different regions to see the page in their preferred language.

Andreas

Thank you for this simple and effective script. I was going crazy not finding the solution. Even ChatGPT did find the correct solution :))

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *