Setting the html language meta tag and/or lang tag is important for search engines like Google and increases Web accessibility.

WordPress does not set the language meta tag, and the html lang tag is only set by some WordPress themes. It is easy to set these, so I suggest you do it. If you have the All in one SEO plugin installed, you can define the language meta tag for all posts, pages and home in the settings. But what if your Blog is multi-lingual? Then you definitely want to set these tags matching the language of a post. Below I will describe how to do this.

Html language meta tag depending on language of post
Html lang tag depending on language of post


Html language meta tag depending on language of post

Description

Here is a WordPress function to add the html language meta tag for different languages. Depending on the categories a post is in, a different language tag will be set.

I searched on the web for a possibility to do this but could not find anything. The WordPress plugin All in one SEO provides functionality to add custom tags, but not to use different once for each post.

Function


<?php
/*
adds the language meta tag to a post. if the post contains the
category "German articel" the language will be "de", otherwise "en".
*/

function addLangMetaTag (){
    $gerCatname = "German articel"; // name of german category
    $postLanguage = "en-US";
    if (is_single()) {
        global $post;
        foreach((get_the_category($post->ID)) as $category) {
            if ($category->cat_name == $gerCatname) {
                $postLanguage = "de";    
            }
        }
        echo "<meta name=\"language\" content=\"" . $postLanguage . "\">";
    }
}
add_filter( 'wp_head', 'addLangMetaTag' );

?>

As one can see, with this function the language meta tag “de” will be added if the post is in the category “German article” (in which all my German articles are), otherwise the tag will be set to “en”.

Usage

To use this function one can add it in the plugins folder or just paste it in the functions.php (residing inside the directory of the active theme). I chose the second option as to not clutter up the plugin screen. If any of your plugins or themes define the html tag lang you might want to disable this, as the lang tag takes precedence over the language meta tag. for example the TwentyEleven theme does this in header.php



Html lang tag depending on language of post

If you are worried of not following the W3C recommendation or want to define the html lang tag depending on post category for some other reason, this can be done as well.

Function

<?php
/*
  function to determin the lang tag to use (depending on categories of a post).
  The output will look like this:
  dir="ltr" lang="the_determined_lang"
  This function is meant to be used to override the language_attributes wordpress function.
 */

function language_tagger_change_html_lang_tag() {
    return "dir=\"ltr\" lang=\"" . language_tagger_determin_lang_tag() . "\"";
}

function language_tagger_determin_lang_tag() {
    $postLanguage = 'en-US'; // default language
    if (is_single()) {
        global $post;
        // determin if a different language should be used:
        foreach ((get_the_category($post->ID)) as $category) {
            if ($category->cat_ID == 'german articel') {
                $postLanguage = 'de';
            }
        }
    }
    return $postLanguage;
}

add_filter('language_attributes', 'language_tagger_change_html_lang_tag');
?>

Usage

I changed the header.php of my WordPress theme directly to include the above code, but I am sure that this can be achieved in a cleaner way as well (for example the theme TventyEleven uses the language_attributes function by default to determine the tag, so just using the above function instead will do it).

The result would look like this on an English WordPress post or on any site:

<html dir="ltr" lang="en-US">
...
<meta name="language" content="en-US">
...

And like this on any German post:

<html dir="ltr" lang="de">
...
<meta name="language" content="de">
...

The code is easily customizable in case you post in different languages or more than just two languages or in case you want to change the tags depending on WordPress tags rather than categories, etc. It can also easily be wrapped up in a WordPress plugin (which I did, get it here: WordPress plugin to change language tag).