Home » Blog » Tutorials » Howto Display Different Widgets Per Language

Howto Display Different Widgets Per Language

March 29th, 2011 by

WPML lets you translate widget titles and the body of text widgets. Sometimes, it’s not enough and you want to have completely different widgets for each language. I’ll show you how to do this using Widget Logic.

Widget Logic is a plugin that allows controlling where widgets display using PHP code. You can use it to specify where widgets are allowed, including which languages they will display in.

For example, supposing that I want to have different feeds in the widget area. There are different ways to do this, but I’ll show how it can be done using Widget Logic and language conditions.

Before we begin, download and activate Widget Logic:
http://wordpress.org/extend/plugins/widget-logic/

Add multiple copies of the widget to the Widget Area

Each of these widgets will be active in only one language, so we need as many copies as the number of languages we have.

The order of these copies doesn’t matter, as only one will be active at any time.

Two RSS widgets, one per language

Add language conditions per widgets

Click to edit each of these widgets and add the language conditions to the Widget Logic field.

WPML's language code

The logic that we add uses WPML’s language code constant – ICL_LANGUAGE_CODE (reference). For example, for English we add:

ICL_LANGUAGE_CODE == 'en'

This condition tells Widget Code to display this widget only on English pages.

We repeat this for all the copies that we created.

The result – different widgets per language

You should expect to see the different widgets per language now.

English page German page

22 Responses to “Howto Display Different Widgets Per Language”

  1. Alberto says:

    Incredible! I searched today “widget” on wpml blog and I found the post published today!
    Really useful…

  2. h.udo says:

    This is fantastic!

    Thanks, guys.

    h.udo
    a happy WMPL user

  3. Fernando says:

    I’ve implemented that solution for a widget I have in my client site, it has several languages, when I create the first widget (ICL_LANGUAGE_CODE == ‘en’) no problem, when i create the second (ICL_LANGUAGE_CODE == ‘es’) no problem , then when i create the third (ICL_LANGUAGE_CODE == ‘pl’) widgets disappears, all of them

    check the DB and all the strings are with status=0 , i can’t figure out why this is hapenning

    any suggestion will be welcome
    Thanks

    • amir says:

      Can you repost this in our technical forum?

      Generally speaking, the string translation has nothing to do with this. You’re creating 3 independent widgets and adding display logic to each.

  4. Sven says:

    The ability to translate content in widgets is exclusive to the commercial version of this plugin yes? Is any widget translatable then, like Recent posts, or Enhanced Recent posts, or even other widgets based on other plugins?

  5. Marc says:

    If I enable the plugin an other plugin called “widget-classes” doesn’t work anymore. Does anyone know that issue?

  6. Gull says:

    hi if we drag another widget in the same area where using it alreay for second language it does not work ……………….plz any help

  7. Beng says:

    I just want to show different text in the title and I don’t want to duplicate two widgets, how do I go about doing that?

  8. Dan says:

    Is there a way to juste make another widget with this pluggin? I need more than only one cause I want to put it in different sidebars… Poosible?

    Thanks

    D.

    • amir says:

      I’m sorry, but I don’t understand your question. If you are using WPML, please start a thread about this in our forum and explain with more details.

  9. Axel says:

    Hello,

    while using the Latest News widget on the french version of the my website with ICL_LANGUAGE_CODE == ‘fr’, the widget displays all posts regardless of the language.

    My website in in French and English.

    When I try it with the Category Post Widget and select the category with logic ICL_LANGUAGE_CODE == ‘fr’, it displays the french title of the widget as expected but not the posts.

    Could you help here please ?

    Thanks

  10. Brandon says:

    I’m a bit confused? is this for widget posts. i purchased WPML for my my theme and it works with pages, categories, menus.

    But how do you get it to work with The Side Bars made for pages and trhe typical Text Widget, Custom Menu widget, etc. that you drop in the side bars? . does this work for what i need? I was hoping for something simple the way you translate regular pages where you have the language flag with the plus signs on the text widget for example and you just fill it in.

    Sorry if i sound like an idiot and thanks for your help!

    • amir says:

      This post explains one way of doing things using the Widget Logic plugin. If you’re using WPML’s String Translation module, you’ll be able to translate Text widgets from the String Translation screen. If you need more help, please post about it in our forum. Make sure to explain exactly what you’re doing so that the support people can advice on the correct path.

      • Brandon says:

        I totally get it. all the text widget translation problems i had are solved.

        Now my new problem is the Custom Menu Widget which does not show up in string translation or on the widget screen. so i have no clue how to deal with this. here is a link to the problem on my site. the custom menu is on the side bar.

        http://optimasystems.ca/pos_division/?page_id=246

        lol

        never mind its solved. the custom menu automatically picked it …. wow i feel dumb. all this time i was looking for a place to make the custom menu widget to work and really all i had to do was just take a look!!!

  11. ragulka says:

    Hey, is it possible for me as a widget coder to make my widget compatible with WMPL in the same way WPML has provided the Multilingual text Widget – so that the user can just have a dropdown with languages and can choose which language this widget should have?

  12. Henrik says:

    Fantastic, just what I’ve been looking for!

  13. Anders F says:

    I have a subscription service in my sidebar. Is it possible to let people subscribe to content only of a single langauge, with WPML string translator OR the Widget logic plugin?

    • Amir says:

      It’s possible, but I can’t tell without seeing how it’s built. Please explain it in our technical forum and the support folks will help. Give them details so that they can explain exactly.

  14. Tony says:

    This is very cool! Only just came across the need to translate a text widget today and a search brought me almost directly to this page. What a simple yet completely functional solution. Great stuff!

Leave a Reply

Please leave here comments about this page only.
For technical support and feature suggestions, head to our forum. We are waiting there!

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>