Hreflang System for Wordpress

by John H
4 minutes

HrefLang Class

This class handles buidling the href lang tags across all of out international Wordpress implementations. The class instance is created in /global/partials/header-footer/header/header.php. The file is referenced in /wp-content/themes/theme/header.php - which is included in every international repo.


The function of the HrefLang class is to build the href lang meta tags on every page of the website. The class is called on every page, then queries the database and returns the countries that page exists on; building the links to that page in alternate regions and languages. This is done to provide search engines reference to the different international and language versions of the site. For example the href lang links for the "company" page would have the Spanish, Chinese, Australian and Singapore region links in the page so that if someone specific to that region was searching for a page, the appropriate link would be provided.

How it works

The entire implementation is accomplished by just instantiating the class. The whole process takes place in the constructor of the class. Stepping through the constructor is enough to understand how the class works. Basically when the class is instantiated the Href lang links are output.

What it does

    <link rel="alternate" hreflang="en" href="" />
    <link rel="alternate" hreflang="en-AU" href="" />
    <link rel="alternate" hreflang="es" href="" />
    <link rel="alternate" hreflang="en-SG" href="" />
    <link rel="alternate" hreflang="zh-CN" href="" />
    <!-- END HREFLANG  -->   
The page loads and hits the class instance. The constructor takes the wordpress $post object - it uses this to make sure that the page being loaded is actually a wordpress page and not just and empty route. Once the page is established it does the following
  • Grabs the Url
  • Cleans the Url (removes any country designation - which is important since this code runs on multiple wordpress installations
  • Gets Server host (also used in building the links)
  • Creates date variables (used to check if the code needs to run)
  • Grabs the current pages country pages from the database
  • Creates urls for use in Curl operations
  • Creates variable with all of the current international urls


  • The getHrefDate() returns the href_page object with includes the id and href_date (the last time it was updated)
  • If the href_date is older tha 2 days old then the script is fully executed - otherwise it will just return the links stored in href-lang-pages db
  • If the href_date is empty - then the page is inserted into the db and the international pages are curl'd and inserted into the db
  • If updating - a loop runs through all of the international urls for the page
    • If the page header returns 200 it is either inserted into the db or skipped (it already exists)
    • If the header returns something besides 200 the page is deleted from the db or skipped
  • If the href_date is newer than 2 days then the links are built using the country codes stored in the db


This class requires 2 tables: href_lang_page and href_lang_pages. href_lang_page includes the page_id, slug, and href_date. The second table href_lang_pages includes id, page_id and country

Related Articles

Feedback Dashboard using Vue.js For Wordpress

I just completed a Feedback system for monitoring if a FAQ post was helpful or not. Every post...

John H John H
4 minutes

Dynamic Field Sorting for Wordpress ACF

A site my employer has uses the Advance Custom Fields plugin for Wordpress. They use the plugin...

John H John H
~1 minute


This is a website I built for a campaign called Transfer Madness.  The campaign focuses on...

John H John H
~1 minute