HTML Lang Declaration

The HTML lang declaration and hreflang tags are two of the many ways used to determine the language of a web page.

The lang declaration is an attribute which is added to the html element of a page. The lang attribute should be equal to the ISO language code of the current page’s language. For example:

<html lang = "en">

The language declaration is also good for when there is a localized version of a website for example if a page’s content is in US English:

<html lang = "en-US">

How To Add A Lang Attribute In WordPress

To add a language attribute in WordPress all you need to do is add the ‘language_attributes’ function to the html tag in the header of your theme:

<html <?php language_attributes(); ?>>

Lang Tags Are Currently Not A Google Ranking Factor

Google does not look at lang tags when crawling websites. However, they are good for usability as they help assistive technologies treat the content of web pages correctly.

hreflang Tags

hreflang tags are used when to declare alternate versions of the current page on a multilingual/multi-locale website. hreflang tags are usually added as link elements in the <head> section of HTML pages.

<link rel=”alternate” hreflang=”fr-ca” href=”http://example.com/fr/” />

You can add multiple alternate language versions for each language published language version.

Adding hreflang Tags In WordPress

The quick and dirty way.

As a quick and dirty fix you can add an hreflang link by hooking to the wp_head function in WordPress:

add_action('wp_head', 'add_HTML_head');
function add_HTML_head(){
	echo '<link rel="alternate" href="http://example.com/fr" hreflang="fr" />';
}

The above function words but things can get messy quickly. On a website with multiple languages you need to track the current page’s language and make sure you’re linking the correct alternates, and preferably do this automatically to avoid making mistakes.

Even Google Search Advocate, John Mueller admits as much:

WordPress Multilingual Plugins

WordPress multilingual Plugins do all the heavy lifting by adding the correct helping you structure your content so you don’t have to keep track of lang declarations and hreflang tags. Just install the plugin, set the language for each post and let the plugin do the rest.

Some of the popular multilungual plugins include:

WPML

PolyLang

GlotPress

TranslatePress

Leave a comment

Your email address will not be published.