Understanding Hreflang in WordPress

Hreflang tags are one of the most critical elements of multilingual web design, but they are also the easiest to break. WordPress plugins often conflict, causing circular redirect loops, missing return tags, or duplicate indexings. In this article, we'll walk through setting up hreflang tags cleanly using a custom functions filter.

Why Avoid Heavy Multilingual Plugins?

Many popular translation plugins create massive database bloat, slow down page load times, and charge expensive monthly subscriptions for basic features. If you are managing your translation manually (which is best for SEO), you only need WordPress to output the correct head links.

The Code: Adding Hreflang via functions.php

You can add clean hreflang output directly to your active theme's functions.php file. Here is a simple snippet that checks the current page and outputs alternate language URLs:

function add_custom_hreflang_tags() {
    $current_url = (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    
    // Example: English site maps to spanish subfolder
    if (strpos($current_url, '/es/') !== false) {
        $en_url = str_replace('/es/', '/', $current_url);
        $es_url = $current_url;
    } else {
        $en_url = $current_url;
        $es_url = str_replace($_SERVER['HTTP_HOST'] . '/', $_SERVER['HTTP_HOST'] . '/es/', $current_url);
    }
    
    echo '' . "\n";
    echo '' . "\n";
    echo '' . "\n";
}
add_action('wp_head', 'add_custom_hreflang_tags');

Be sure to validate your output using Google Search Console's targeting reports to confirm Google recognizes the relationships properly.