hreflang done right on a multilingual WordPress site
hreflang tells search engines which pages are translations of each other, so that a French visitor gets the French page in the results and not the English one. It does not affect rankings and is not a protection against “duplicate content”; it only connects versions of the same page. Google ignores the tags when the set is incomplete or contradicts itself, and that happens easily.
Problem: A site has English, French and Spanish versions. Search Console shows French pages in the English results and reports “Alternate page with proper canonical tag” in odd places. The theme prints hreflang only on single posts, only towards the translations, and the French pages have a canonical URL that points to the English page.
Solution: Print a complete set of <link rel="alternate" hreflang> tags on every translated page, including the page itself and an x-default, make sure every version links back to all others, and keep a self-referencing canonical on each language version.
The rules Google checks
- Reciprocal: if the English page names the French page as its alternate, the French page must name the English page too. One-sided links are ignored.
- Self-reference: each page lists itself in its own set.
- Canonical per language: the French page’s canonical is the French URL. A canonical to the English page says “this page is a copy, ignore it”, and then its hreflang is ignored too.
- Valid codes: ISO 639-1 language, optionally with an ISO 3166-1 region:
fr,pt-BR,en-GB.en-UKis invalid (the code is GB), and a region alone likebeis a language (Belarusian). - Indexable targets: the URLs return 200, are not noindex and not redirected.
The tags from PHP
The example assumes that each post stores the IDs of its translations in a meta field _ha_translations, an array like [ 'en' => 12, 'fr' => 57, 'es' => 90 ], saved identically on all three posts. Translation plugins keep such a map in their own tables and usually print the tags themselves; the code is for a custom setup.
add_action( 'wp_head', 'ha_hreflang_tags', 1 );
function ha_hreflang_tags(): void {
$default = 'en';
$urls = array();
if ( is_front_page() ) {
$urls = array(
'en' => home_url( '/' ),
'fr' => home_url( '/fr/' ),
'es' => home_url( '/es/' ),
);
} elseif ( is_singular() ) {
$map = get_post_meta( get_queried_object_id(), '_ha_translations', true );
if ( ! is_array( $map ) ) {
return;
}
foreach ( $map as $lang => $post_id ) {
if ( 'publish' === get_post_status( $post_id ) ) {
$urls[ $lang ] = get_permalink( $post_id );
}
}
}
if ( count( $urls ) < 2 ) {
return; // no translation, no hreflang
}
if ( isset( $urls[ $default ] ) ) {
$urls['x-default'] = $urls[ $default ];
}
foreach ( $urls as $lang => $url ) {
printf( '<link rel="alternate" hreflang="%s" href="%s">' . "\n", esc_attr( $lang ), esc_url( $url ) );
}
}
Because the map is the same on all versions, every version prints the same set, which makes the links reciprocal and self-referencing automatically. Only published translations are included, so a draft never appears as an alternate. x-default is the page for visitors whose language is not in the list; usually the English version or a language selector page.
Check the canonical
add_filter( 'get_canonical_url', 'ha_canonical_stays_in_language', 10, 2 );
function ha_canonical_stays_in_language( string $url, WP_Post $post ): string {
return get_permalink( $post ); // never the URL of another language version
}
The core canonical is already the post’s own permalink; this filter matters when an SEO plugin or old code sets the “original” language as canonical. SEO plugins have their own canonical filters, so check the final HTML of a French page.
Test
Open a French page, view the source and compare its set with the English page: same URLs, same codes, each page included. Search Console no longer has a dedicated hreflang report, so check a sample of URLs with the URL Inspection tool, which shows the canonical Google chose; it should be the page itself.
NOTE: For thousands of pages, hreflang can also live in the XML sitemap (xhtml:link entries per URL) instead of the HTML head, which keeps the pages lighter; use one method, not both with different data. A setup with one site per language in a Multisite network: WordPress Multisite with hreflang.