WordPress, qTranslate, Custom Menu Item Links

On the Food Empowerment Project site, we use mqTranslate, a successor to qTranslate, to manage content in English and Spanish. In conjunction with qTranslate Slug, we get specific urls for pages in English and Spanish, a language switching button in the header, and menus & widgets reflecting the currently selected language.

Menu items which link to pages reflect not only the right language for the menu text, but also automagically link to the correct url for the current language. The challenge I found was with custom menu items. They show the correct text for the menu item by language, but because of the way WordPress handles the “url” of the custom menu item, you can’t use multiple languages in that field.

The right way to fix this, I suppose, would be a custom plugin which extends the custom menu item to allow multiple URLs by language, and then updates the admin interface to show the appropriate URL field for the current language, just as qTranslate does to show the appropriate menu text for the current language (inside the dashboard & when output).

But I didn’t have time for that.

The goal was to add a link in primary navigation menu to another project of Food Empowerment Project, VeganMexicanFood.com. That site has a different url for English and Spanish, but is an external link.

The simple answer I found was to use the wp_nav_menu_objects filter:

function vmf_menu_links($items, $args) {
     if(qtrans_getlanguage() == 'en') {
          return $items;
     foreach ($items as $item) {
          if ($item->ID == '512') {
               $item->url = 'http://www.veganmexicanfood.com/index_es.htm';
     return $items;
add_filter('wp_nav_menu_objects', 'vmf_menu_links', 10, 2);

This filter first checks to see if we’re in English – if we are, it just returns the items unchanged. If we aren’t, that means we’re in Spanish (we only have 2 languages). In that case, loop through the items and when you find the item with ID 512 (the link to Vegan Mexican Food), change the url of that item.

Of course this hack hardcodes the ID of the menu item targeted, as well as the url to use. But for a quick hack it works well, and since the site is heavily cached I wasn’t too worried about performance impact. (You can find the ID of the item easily by looking at the CSS where it is output in the menu).

It could be extended as a switch statement to look for multiple languages and substitute appropriate urls.