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

37 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!

  15. marcos says:

    Amir,

    I have Ubermenu plugin. It shows widgets content within a Megamenu. Is it possible to show Portuguese or English widgets inside the same Megamenu ?

    How can I use the Widget logic with Ubermenu ?

    Website address: http://www.sinditabaco.com.br/novosite.

    Thank you very much.

    • Amir says:

      I really don’t know, because I’m not familiar with that plugin. If you can narrow this down to specific things in the PHP of that plugin, our support folks will be able to help in the forum.

  16. Filippo says:

    It works fine, have a good job

  17. steve says:

    “WPML lets you translate widget titles and the body of text widgets. ”

    … I dont see any way to translate the widgets on my widget admin panel… I have all my widgets and titles in EN and I want to get also a FR version. How can I do without installing the plugin here ? thanks

    • Amir says:

      Generally speaking, you should do this using WPML’s String Translation. It’s difficult to tell without the details. Please start a thread about this in our technical forum.

      Tell what theme you’re using, which widgets you’re trying to translate and what you’re trying to do in WPML. Our support people will help.

    • steve says:

      thanks Amir, I did create a ticket in the support as advised, have a nice WE

  18. Tom says:

    Hi

    I’m using the Widget Logic plugin. My main language is Danish and the url structure is just normal when on danish pages and the second language is English and the url change to /en/ when on english pages.

    Using the above plugni work fine with my second language (english) using ICL_LANGUAGE_CODE == ‘en’ but the widgets that I use for the danish pages do not show. I use ICL_LANGUAGE_CODE == ’da’ for the widgets that I want to show on danish pages.

    Am I doing some thing wrong?

    • Amir says:

      I’m really not sure what might be the reason. Have you double checked the language code? You might want to echo the ICL_LANGUAGE_CODE constant and check that it reports the correct value.

  19. Tom says:

    Not quit sure what you mean by “You might want to echo the ICL_LANGUAGE_CODE constant and check that it reports the correct value.”??

  20. Mich says:

    Hello
    I use the template in Rocketthemes with Gantry how Configuration Manager (in wordpress), how it is possible to decompose two languages ​​at the same time.
    How do your with Widgets with this setup, do you have a tutorial explaining this problem, though.
    Thank you for your help
    Mich

  21. Ricardo I. says:

    I’ve followed the instructions here with Widget Logic and added the language-specific code to my contact form (instructions for which I found on WPML’s site, too), yet both my Spanish (es) and English (en) widgets appear on both English and Spanish pages.
    Also, when I try to set the multilingual option to one language or the other, it reverts to “Multilingual” when I save the widget.
    Thanks for your help!

  22. Nikos says:

    Hello,
    I try to translate the widget by following the above instructions but I have a problem.

    The site languages is Greek (main language) and English.
    I make two additional as in example and put the code like this ICL_LANGUAGE_CODE == ‘en’ and ICL_LANGUAGE_CODE == ‘el’ but the widgets is not appear in any of the two languages.

    Can you help me;
    Thanks

    • Amir says:

      This is pretty old and we haven’t tested WPML with Widget Logic recently. WPML now includes its own ‘multilingual text’ widget, which lets you choose the language. Have you tried that?

      • Nikos says:

        Thanks for the quick reply!
        My problem is to Display Different Widgets Per Language.
        I use the Widget Logic plugin like the above instructions but its not work for me.
        No i don’ t try the multilingual text’ widget. Can you send me instructions to use it ?
        Forgive me for my bad English

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>