Skip to content

Hreflang Attributes

Web Lifter edited this page Jan 31, 2025 · 8 revisions

Hreflang Implementation in WordPress

This page explains how the hreflang attributes are dynamically added to the <head> section of a WordPress website using PHP. These implementations ensure that search engines serve the correct language version of your website to users based on their region and language preferences.

Code Explanation

1. Basic Hreflang Implementation

<?php
// Access the global $wp object
global $wp;

// Retrieve the current page URL dynamically
$current_url = home_url(add_query_arg(array(), $wp->request));

// Define language versions and their corresponding URLs
$translations = array(
    'en-au' => 'https://example.com/' . $wp->request,
    'fr'    => 'https://example.com/fr/' . $wp->request,
    'de'    => 'https://example.com/de/' . $wp->request,
    'es'    => 'https://example.com/es/' . $wp->request
);

// Loop through translations and generate hreflang links
tforeach ($translations as $lang => $url) {
    echo '<link rel="alternate" hreflang="' . esc_attr($lang) . '" href="' . esc_url($url) . '">' . "\n";
}
?>

<!-- Default language for users without a specific match -->
<link rel="alternate" hreflang="x-default" href="<?php echo esc_url($translations['en-au']); ?>">

Code Breakdown:

  • global $wp; → Accesses the global $wp object to retrieve page request data.
  • $current_url = home_url(add_query_arg(array(), $wp->request)); → Dynamically constructs the full URL of the current page.
  • $translations → Defines a mapping of language codes to their respective URL versions.
  • foreach ($translations as $lang => $url) → Loops through translations and prints <link> elements with the appropriate hreflang attributes.
  • <link rel="alternate" hreflang="x-default" href="..."> → Specifies the default page version when no language match is found.

2. Hreflang with Dynamic Slugs

<?php
// Access the global $wp object
global $wp;

// Retrieve the current page URL dynamically
$current_url = home_url(add_query_arg(array(), $wp->request));

// Detect current page slug
$current_slug = $wp->request;

// Define available translations
$translations = array(
    'en-au' => 'https://example.com/' . $current_slug . '/',
    'fr'    => 'https://example.com/fr/' . $current_slug . '/',
    'de'    => 'https://example.com/de/' . $current_slug . '/',
    'es'    => 'https://example.com/es/' . $current_slug . '/'
);

// Loop through translations and generate hreflang links
foreach ($translations as $lang => $url) {
    echo '<link rel="alternate" hreflang="' . esc_attr($lang) . '" href="' . esc_url($url) . '">' . "\n";
}
?>

<!-- Default language for users without a specific match -->
<link rel="alternate" hreflang="x-default" href="<?php echo esc_url($translations['en-au']); ?>">

Code Breakdown:

  • global $wp; → Accesses the global $wp object.
  • $current_slug = $wp->request; → Retrieves the current page slug, ensuring URLs maintain their structure.
  • $translations → Defines the language versions for each page dynamically by appending the slug.
  • foreach ($translations as $lang => $url) → Iterates through the array and prints <link> elements with hreflang attributes.
  • <link rel="alternate" hreflang="x-default" href="..."> → Specifies the default page version.

When to Use Each Implementation

Implementation Use Case
Basic Hreflang Implementation Best for structured websites where translated versions exist at predefined paths (e.g., /fr/, /de/).
Hreflang with Dynamic Slugs Recommended when translated pages have the same slug across different languages. Ensures URL consistency and SEO benefits.

Why Use Hreflang?

  • Helps search engines display the correct language version of your website to users.
  • Improves international SEO by avoiding duplicate content issues.
  • Prevents Google from incorrectly indexing pages in the wrong language.

Need Help?

If you have any issues with the implementation, feel free to open an issue in this repository.