Beantin webbkommunikation

Beantin Beta Blog

Experiments, investigations, and tests of web things
June 27, 2014

WordPress: setting the language on individual pages

WordPress by default has only one language setting. Setting up a full blown multi-language site might be overkill. Here is how you can get the correct language on each blog post and on each page.

There are a number of important (technical) reasons to set the correct language for your content. One of them is accessibility and screen readers. They use the lang attribute in the html tag to decide how to read the content. Another is search engines. Some of them make use of the content-language meta tag in deciding the language of the content.

Setting it correctly is never going to be a bad thing for your content!

How to do it

  1. Create a page. You can have this hidden/not indexed or visible in your menu.
  2. Add the code show below to your functions.php file.
  3. Change the page ID of 565 in the code to the page ID of the page you just created
  4. Change the “postlanguage” of en-GB to your default language code.
  5. Change the “postlanguage” of sv to the language you want for these pages
  6. On any page of content in your secondary language, set the parent to be the page you created in the first step
/**
* Return the ID of the root parent page
* Borrowed from http://www.stevendobbelaere.be/get-the-current-pages-parent-page-id-in-wordpress/
*/
function get_top_parent_page_id() { 
    global $post; 
 
    if ($post->ancestors) { 
        return end($post->ancestors); 
    } else { 
        return $post->ID; 
    } 
}

/**
* Adapted from http://software-talk.org/blog/2012/02/wordpress-html-language-meta-tag-depending-on-post-language/
*/
function addLangMetaTag (){
    $postLanguage = "en-GB";
    if (is_page()) {
$svPageID = get_top_parent_page_id(); // ID of parent page
            if ($svPageID == "565") { // ID of the "på svenska" page
                $postLanguage = "sv";    
            }
        echo "<meta http-equiv=\"content-language\" content=\"" . $postLanguage . "\">";
    }
}
add_filter( 'wp_head', 'addLangMetaTag' );

/**
* Adapted from http://software-talk.org/blog/2012/02/wordpress-html-language-meta-tag-depending-on-post-language/
*/
function language_tagger_change_html_lang_tag() {
    return "dir=\"ltr\" lang=\"" . 
language_tagger_determin_lang_tag() . "\"";
}

function language_tagger_determin_lang_tag() {
    $postLanguage = 'en-GB'; // default language
    if (is_page()) {
$svPageID = get_top_parent_page_id(); // ID of parent page
if ($svPageID == "565") { // ID of the "på svenska" page
                $postLanguage = "sv";    
            }
}
    return $postLanguage;
}

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

This article, which I used as inspiration, shows how to implement the same kind of thing for blog posts (based on a specific category).

This article I grabbed the “find parent” function from.

Related Posts Plugin for WordPress, Blogger...

Tags

Filed under Configuration, Wordpress.

Tags: , ,

Theme Beantin created by James Royal-Lawson. Privacy Policy