Hreflang System for WordPress
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
<!-- LISTING HREFLANG ALTERNATIVE PAGES --> <link rel="alternate" hreflang="en" href="http://domain.com/" /> <link rel="alternate" hreflang="en-AU" href="http://domain.com/au/" /> <link rel="alternate" hreflang="es" href="http://domain.com/es/" /> <link rel="alternate" hreflang="en-SG" href="http://domain.com/sg/" /> <link rel="alternate" hreflang="zh-CN" href="http://domain.com/zh/" /> <!-- 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