individual wordpress language

If you are running a website in one language like me, but you need to sometimes publish the content in a different individual language over page or post and you want it SEO friendly, you have to set the correct individual language for this content.

I did some research and here is a working PHP code which has to be added into the function.php file.

The solutions below are really good if you have no plans to translate whole website.

Individual language for posts

You just have to set the new category, eg Czech Articles. So all articles in Czech Articles will have the correct Czech (or your preferred) language. Please, see the comments after “//”

<?php
function addLangMetaTag (){
$gerCatname = "Czech Articles"; //name of Czech Articles category
$postLanguage = "cs"; //language
if (is_single())
{global $post;foreach((get_the_category($post->ID)) as $category)
{if ($category->cat_name == $gerCatname)
{$postLanguage = "cs";}} //language
echo "<meta name=\"language\" content=\"" . $postLanguage . "\">";}}
add_filter( 'wp_head', 'addLangMetaTag' );
?>

Individual language for pages

This is little bit longer and “busy looking” code for pages. You have to insert (change) the page ID and you’re ready.

<?php
function get_top_parent_page_id() {global $post;
if ($post->ancestors){return end($post->ancestors);} else {return $post->ID;}}
function addLangMetaTag ()
{$postLanguage = "en-GB";
if (is_page()) {$svPageID = get_top_parent_page_id(); // ID of parent page
if ($svPageID == "455") { // ID of the page
$postLanguage = "cs";} // czech language
echo "<meta http-equiv=\"content-language\" content=\"" . $postLanguage . "\">";}}
add_filter( 'wp_head', 'addLangMetaTag' );
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 == "455"){ // ID of the page
$postLanguage = "cs";}} // czech language
return $postLanguage;}
add_filter('language_attributes','language_tagger_change_html_lang_tag');
?>

Credits: software-talk.org, beta.beantin.se

Author: Milan Lipowski

Milan is a web designer & developer and stock photographer. He also has over 20 years experience with hardware and software computer servicing for individuals, and small & middle-size companies.