SEO & Blogging

Add Hreflang Tags in WordPress Without a Plugin

If you publish the same page in English, Urdu, and Spanish, search engines need a clear signal about which version belongs to which audience. Hreflang tags provide that signal by connecting several URLs as language or region versions of one piece of content.

You do not need a heavy multilingual plugin just to print these tags. A small PHP snippet hooked into wp_head lets you add hreflang tags in WordPress without a plugin while keeping full control over the output. This guide covers the rules first, then two code patterns for common URL structures.

What does hreflang actually do?

Hreflang is an attribute on a link element in the head of a page. Each tag names one alternate version, gives its language code, and points to its full URL. Together, the tags form a small map that search engines read before choosing which version to show.

For example, a services page might exist in English at the root, in Urdu under a ur prefix, and in Spanish under an es prefix. Hreflang groups those URLs so Urdu searchers see the Urdu version and Spanish searchers see the Spanish version. Google explains this targeting signal in its Search Central documentation.

Do you actually need hreflang tags?

You need hreflang only when the same content exists in two or more language or region versions, such as English and Urdu editions of one article, or en-US and en-GB pages with different pricing. A single language site does not need hreflang at all, and adding it there only creates noise.

Changing your site language is a different task. Switching WordPress to Urdu changes one site to that language, but it does not create alternate versions of your pages. If that matches your situation, read our guide on how to change your WordPress site language before deciding that you need a multilingual setup.

What are the core hreflang rules you must follow?

Hreflang fails quietly when one rule breaks, and search engines may ignore the whole group. Keep these rules in mind before writing any code:

  • Reciprocity is mandatory: every version must list all versions in the group, including itself. One missing return tag can invalidate the set.
  • Use absolute URLs: each href must include the protocol and domain, never a relative path.
  • Targets must be live and indexable: every listed URL should return a 200 status, allow indexing, and avoid redirects.
  • Keep self referencing canonicals: each version canonicalizes to itself, never to another language version.
  • Add x-default: this entry covers visitors whose language matches none of your versions.
  • Print the identical set everywhere: every page in a group outputs the same list of tags.

Each page confirms the others exist, so one broken promise can undermine the whole group. Testing matters as much as the code.

How do you add hreflang tags in WordPress without a plugin?

The cleanest manual method hooks a small function into wp_head so WordPress prints the tags in the head of each relevant page. If hooks are new to you, our explainer on how WordPress actions and filters work covers the basics, and the WordPress developer reference documents the wp_head action.

Put the snippet in a child theme functions.php file or the Code Snippets plugin, never in a parent theme that updates will overwrite. Take a full backup first and test on a staging site, because a syntax error in functions.php can take a live site offline.

Pattern 1: How do you handle predictable language prefixes?

Many sites keep the same slug across languages and change only the prefix. This function strips any known prefix from the current path, rebuilds each version with home_url, and prints the full set plus x-default:

<?php
add_action( 'wp_head', 'vutg_print_hreflang_tags' );

function vutg_print_hreflang_tags() {
    if ( is_admin() || is_feed() ) {
        return;
    }

    $languages = array(
        'en' => '',
        'ur' => 'ur',
        'es' => 'es',
    );

    $path = trim( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' );
    $segments = $path ? explode( '/', $path ) : array();

    if ( ! empty( $segments ) && in_array( $segments[0], array( 'ur', 'es' ), true ) ) {
        array_shift( $segments );
    }

    $base_path = implode( '/', $segments );

    foreach ( $languages as $code => $prefix ) {
        $url_path = $prefix ? $prefix . '/' . $base_path : $base_path;
        $url = $url_path ? home_url( '/' . trim( $url_path, '/' ) . '/' ) : home_url( '/' );
        echo '<link rel="alternate" hreflang="' . esc_attr( $code ) . '" href="' . esc_url( $url ) . '" />' . "\n";
    }

    $default_url = $base_path ? home_url( '/' . trim( $base_path, '/' ) . '/' ) : home_url( '/' );
    echo '<link rel="alternate" hreflang="x-default" href="' . esc_url( $default_url ) . '" />' . "\n";
}

Note the safeguards: the function skips admin and feed output, and it escapes codes with esc_attr and URLs with esc_url. Use this pattern only when every path truly exists in every language. Advertising a Urdu or Spanish URL that returns a 404 error weakens the whole group.

Pattern 2: What if translated slugs are completely different?

Translated slugs often use words from their own language, so prefix logic cannot guess them. Store the counterparts in a slug map instead. This function runs only on singular pages and prints tags only when the current path belongs to a mapped group:

<?php
add_action( 'wp_head', 'vutg_print_mapped_hreflang_tags' );

function vutg_print_mapped_hreflang_tags() {
    if ( ! is_singular() ) {
        return;
    }

    $map = array(
        array(
            'en' => '/about-us/',
            'ur' => '/ur/hamare-bare-mein/',
            'es' => '/es/sobre-nosotros/',
        ),
        array(
            'en' => '/contact/',
            'ur' => '/ur/rabta-karen/',
            'es' => '/es/contacto/',
        ),
    );

    $current_path = '/' . trim( parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ), '/' ) . '/';
    $group = null;

    foreach ( $map as $candidate ) {
        if ( in_array( $current_path, $candidate, true ) ) {
            $group = $candidate;
            break;
        }
    }

    if ( ! $group ) {
        return;
    }

    foreach ( $group as $code => $path ) {
        echo '<link rel="alternate" hreflang="' . esc_attr( $code ) . '" href="' . esc_url( home_url( $path ) ) . '" />' . "\n";
    }

    echo '<link rel="alternate" hreflang="x-default" href="' . esc_url( home_url( $group['en'] ) ) . '" />' . "\n";
}

The map needs a new entry for each translated page, but it never advertises a version that does not exist. For a small site with a handful of translated pages, that accuracy is worth the manual upkeep.

Which language codes should you use?

Hreflang values combine an ISO 639-1 language code with an optional ISO 3166-1 Alpha 2 region code. Examples include en, en-US, en-GB, ur, ur-PK, and es. Add a region only when content truly targets it, such as es-ES and es-MX editions with different wording.

Never invent codes. A value such as en-UK is invalid because the correct region code is GB, and invalid values are simply ignored.

Where else can hreflang live?

Head tags are one of two accepted homes for hreflang. The other is your XML sitemap, where each URL entry lists its alternates. Both methods work, but pick one method only. Mixing head tags on some pages with sitemap entries on others creates mismatched sets that search engines may disregard.

How do you avoid conflicts with multilingual and SEO plugins?

Before adding code, view the source of one translated page and search for the word hreflang. Polylang and WPML already print hreflang for translations they manage, and some SEO plugins add it too. If correct tags already exist, leave them alone instead of printing a second set.

If a plugin prints flawed tags you cannot fix in its settings, disable that feature first so your snippet becomes the single source of truth.

How do you test hreflang tags properly?

Testing takes a few minutes and catches nearly every failure. Run this checklist for each language group:

  • View the source on every version: confirm each page prints the identical set, including its own entry and x-default.
  • Open every listed URL: each target must load directly with a 200 status, with no redirect chain or error.
  • Check canonicals and indexing: each version canonicalizes to itself and carries no noindex instruction.
  • Use Search Console: inspect the URLs and review international targeting information where your property offers it.

If Search Console is not connected yet, follow our walkthrough to add Google Search Console to WordPress first. Reliable hosting matters here too, since slow servers can make healthy URLs look broken to crawlers. Our web hosting guide compares options that keep multilingual sites stable.

What mistakes break hreflang most often?

Most failures come from a short list of repeat mistakes:

  • One way tags: the main language page lists translations that never list anything back.
  • Bad targets: tags point to URLs that redirect, error out, or carry a noindex tag.
  • Cross language canonicals: translated pages canonicalize to the English version and ask search engines to ignore them.
  • Missing x-default: visitors outside your listed languages get no fallback signal.
  • Wrong output locations: tags appear in admin screens or feeds. Guard the function with conditions such as is_singular and admin or feed checks.

Fix problems in the shared snippet rather than patching pages one by one, so one correction repairs the whole group.

When is a separate site per language a better fit?

Sometimes each language needs its own design, team, or workflow. WordPress Multisite then lets you run separate sites from one dashboard while keeping content apart. Our overview of how WordPress Multisite works explains when that structure helps and when it adds needless complexity.

Hreflang still connects matching pages across those sites, using stored full URLs instead of home_url, with the same reciprocity and canonical rules.

Frequently Asked Questions

Does a single language WordPress site need hreflang tags?

No. Hreflang helps only when equivalent content exists in two or more language or region versions. A one language site gains nothing from these tags, so skip the code and invest in content and internal links instead.

Can incorrect hreflang tags hurt my rankings?

Broken hreflang is usually ignored rather than punished. The real cost is showing the wrong language version to searchers, which raises bounce rates and wastes translated content. Correct reciprocal tags remove that confusion.

Should hreflang go in the page head or the XML sitemap?

Either location works, but choose only one. Head tags are easier to inspect in page source, which suits most WordPress sites. Sitemap entries suit very large sites. Mixing both creates conflicting sets.

Does hreflang replace canonical tags?

No. Each language version keeps a self referencing canonical pointing to its own URL, while hreflang connects it to its counterparts. A canonical aimed at another language version undermines both signals.

Last updated: December 2026.

Hostinger - Get 20% OFF

Habib ur Rehman

Hello! I'm the founder of VU Tech Geek. As a BSCS graduate and experienced WordPress professional, I'm passionate about technology and education. My Virtual University journey has given me valuable insights and knowledge, which I love to share with fellow students.… More »

Related Articles

Leave a Reply

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

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker