( versione italiana)

Multilingual WordPress 3.0

After a brief trial stage of this new release of WordPress, here is a new post about what I had to customize, why I had to do it and what is the final outcome of the operation.

With my previous blog  I proposed, as a case study, to implement a multilingual blog which could be easy to maintein. To get this result, after many searches, I resorted to the xLanguage plugin. At first this appeared as the right choice, but at last – also due to the coupling whit other WordPress plugins, especially whit WP Super Cache – it revealed to be tricky (e.g., if the cache was regenerated by some crawler’s visit, the resulting static pages were made up by mixed localizations, so I had to clear and regenerate the cache by hand) and difficult to keep working when coupled with other plugins, because I had to patch many plugins to work with xLanguage tags (but I had to reapply my patches at every update of the plugins). Frustrating and not very effective.

With the release of WordPress 3.0 and the possibility to enable its multisite mode, I wanted to try to manage the different languages of the blog as distinct subsites, with a common theme and plugins, but correlating the blogs and the article/pages one to each other based on their languages using a pair of custom fields to be accessed by some minimal customizations to the theme.

Here you find the few lines of code required to obtain what I wanted, with the path to the file to edit in bold face.

wp-content/themes/MY_THEME/functions.php:

if ( ! function_exists( 'alternative_language_link' ) ) :
/**
 * Prints HTML link to english or italian version of page or post.
 * @author Ivan Iraci
 *
 */
function alternative_language_link($id) {
  if (get_post_meta($id, 'english-version', true) != "") {
    ?>
    <a href="<?php echo get_post_meta($id, 'english-version', true); ?>">
    (<span>&nbsp</span>english version)
    </a>
    <?php
  } else if (get_post_meta($id, 'italian-version', true) != "") {
    ?>
    <a href="<?php echo get_post_meta($id, 'italian-version', true); ?>">
    (<span>&nbsp</span>versione italiana)</a>
    <?php
  }
}
endif;

if ( ! function_exists( 'alternative_language_site' ) ) :
/**
 * Prints HTML link to english or italian version of the site.
 * @author Ivan Iraci
 *
 */
function alternative_language_site() {
  if (get_locale() == "it_IT") {
    $style = "english-version";
    $other_language = "english version";
    $other_link = get_blogaddress_by_id(2);
  } else if (get_locale() == "en_US") {
    $style = "italian-version";
    $other_language = "versione italiana";
    $other_link = get_blogaddress_by_id(1);
  }
  print '<a href="'.$other_link.'"><span>&nbsp;</span>'.$other_language.'</a>';
}
endif;

Having these two simple functions at my disposal, knowing the english/italian site id and having added the custom field italian_version (in the english article/page, containing the url to the italian version) or english_version (in the italian article/page, containing the url to the english version), all I had to do was adding the link to the alternative versions.

E.g., to render the link to the alternative language of the site, I simply had to edit header.php.

wp-content/themes/MY_THEME/header.php:

<?php echo alternative_language_site(); ?>

On the other hand, to render the alternative language link of the post/page, I had to edit the following files.

wp-content/themes/MY_THEME/loop.php,
wp-content/themes/MY_THEME/onecolumn-page.php,
wp-content/themes/MY_THEME/page.php,
wp-content/themes/MY_THEME/single.php:

<?php alternative_language_link(get_the_ID()); ?>

Really easy and effective.

If the languages were more than two, the code would be more complicated, but not so much.

This solution enables you to carry out some SEO and analytics operations targeted specifically to each language, something impossible to do with xLanguage and the previous version of WordPress. More over, this could let you part the management of rights and policies over the single languages to different teams.

Briefly, a notably progress at the cost of the edit of a very slight portion of the code.

Have a good hacking!

2 thoughts on “Multilingual WordPress 3.0”

  1. Hi Ivanhoe,

    Thanks very much for this code. I’m building exactly the same with a bilingual website in wordpress multisite, and was looking for something like this. I had to adapt your code a little bit to get the links working. In your href in the above code, you only echo the id. I had to find a way of creating a link to the other website based on the id in the custom field.
    Probably this can be done more elegantly, but I’m glad I have this working!
    I can see you have it working also, and I am curious how you did this.


    function alternative_language_link($id) {

    if (get_post_meta($id, 'langid', true) != "") {

    $id_other = get_post_meta($id, 'langid', true);

    if (get_locale() == "nl") {
    switch_to_blog(2);
    $the_other_link = get_permalink( $id_other );
    restore_current_blog();
    print ('Engels');

    } elseif ( get_locale() == "en") {
    switch_to_blog(1);
    $the_other_link = get_permalink( $id_other );
    restore_current_blog();
    print ('Nederlands');
    }
    }

    }
    endif;

    1. I’m happy that my post was useful for someone! 🙂

      As you can read in my article, get_post_meta($id, ‘italian-version’, true) doesn’t return an id but the string containing the URL to the italian version of the article.

      For completeness’ sake, this article’s custom field ‘italian-version’ contains the value ‘http://freelosofia.org/blog/2010/08/26/wordpress-3-0-multilingue’.

      Maybe less smart than your solution, but I was in a hurry and it simply worked. 😀

Leave a Reply

Your email address will not be published. Required fields are marked *