How to localize WordPress pages


Sometimes I am surprised by the power of WordPress. To be honest, I don’t like the style of coding done in WordPress. Too many global functions… but on the other hand, people managed it somehow to create a flexible and extensible product. I don’t know how it is to make huge projects with WordPress, but for my little own blog it gave me everything I need so far. This time I wanted to create localized pages, which were served by PIWI before. In order to reduce the overhead for my website, I decided to use WordPress as one and only the system. The project is updated in regular periods and PIWI is not. It has tons of plugins and PIWI not. However, PIWI is more to my taste of professional software and I will continue to use it on more enterprise developments.

Localization does not work out of the box in WordPress. But the concepts in WordPress allow to use a combination of custom fields and themes to make this work. I refused to translate all my blog posts and for only pages it is very straightforward. First I created my pages in the WordPress pages section. In the Custom fields I used a new meta key named “language” and put as value “de” or “en” for german or english.

Custom fields for localization

I couldn’t use the Pages Widget, because I need to generate my navigation out of the selected language. Not to conflict with my current implementation I duplicated my current theme and used the preview function to work on it (on a live system, which is a really great feature :-) for prime time programming). I should mention that I created two pages, one for each language, only stating that the language has been changed. Next step was to include the language flags in my theme (header.php in my case). I linked the flags to my newly created “language switched” pages. Additionally I have added the param language=de (or en for english) to the link. My complete link for the german language is as follows:

Now I am able to link between the languages. In my header.php I wrote:

include(TEMPLATEPATH . '/grobmeier-session.functions.php');

I started the session myself - it seems WordPress frontend doesn’t use session at all. In the included new php grobmeier-session.functions.php I create a function for storing my language key. This function has been stolen and modified from PIWI:

function grobmeier_getUserLanguage() {
    $supported[] = 'de';
    $supported[] = 'en';

   if (isset ($_GET['language'])) {
       if (in_array($_GET['language'], $supported)) {
            $_SESSION['language'] = $_GET['language'];

    if (!isset ($_SESSION['language'])) {
        $_SESSION['language'] = 'en';
    return $_SESSION['language'];

Now I just need to generate the navigation. I’ll do this with a prepared function of WordPress. On the correct place in my header.php I wrote:

$args = array(
   'meta_key' => 'language',
   'meta_value' =>  grobmeier_getUserLanguage());
wp_list_pages( $args );

As you might see, the navigation is build on the user language setting. And that’s all - you’ll deliver localized versions of your pages from now on. I should mention this works with 2.9.2 and I think earlier versions too. You just need to create pages with the correct custom field setting.

Tags: #Open Source #PHP #WordPress