How to Add Custom Language Switcher Using WPML Plugin of WordPress

I have just modified the custom language switcher for one of my websites that uses multi language feature by using WPML plugin and Atahualpa theme. I saw the related code to implement it via this following article: Custom Language Switcher. Unfortunately, since WPML has upgraded its version for several times, the “languages_list_footer()” function in that article should be changed in order to compatible with the last version of WPML, including the “lang_native_hidden” and “lang_translated_hidden” parameter that will be used in the “icl_disp_language” function. So here is the modification code that I have created in order to display it in the footer of my theme.

  1. Put this following code to your functions.php file of your current theme:

    // In order to display language selector of WPML plugin in footer of theme
    function languages_list_footer(){
        $languages = icl_get_languages('skip_missing=0&orderby=code');
        if($l['icl_lso_native_lang']){
            $lang_native_hidden = false;
        }else{
            $lang_native_hidden = true;
        }
        if($l['icl_lso_display_lang']){
            $lang_translated_hidden = false;
        }else{
            $lang_translated_hidden = true;
        }    
        if(!empty($languages)){
            echo '<div id="footer_language_list">';
            foreach($languages as $l){
                if($l['country_flag_url']){
                    echo ' <a href="'.$l['url'].'">';
                    echo '<img src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" /> ';
                    if(!$l['active']) echo '</a> ';
                }
                echo ' <a href="'.$l['url'].'">';
                echo icl_disp_language($l['native_name'], $l['translated_name'],$lang_native_hidden);
                if(!$l['active']) echo '</a> ';
            }
            echo '</div>';
        }
    }
  2. Open your header.php file of your current theme, and find this code:

    <?php } ?>
    <?php include (TEMPLATEPATH . '/functions/bfa_meta_tags.php'); ?>

    before the first line of that code, please insert this following code:

    <link rel="stylesheet" href="<?php echo WP_PLUGIN_URL; ?>/sitepress-multilingual-cms/res/css/language-selector.css" type="text/css" media="all" />

    this is important, in order to include the .css file belongs to WPML plugin.

  3. The next step, you have to call that function in your sidebar or any bar of your current theme. For example, I use Atahualpa theme, then I put this code calling in the “footer.php” file:

    <?php languages_list_footer(); ?>

    after this following line:

    <?php wp_footer(); ?>
  4. If you want to see the output or demo of this modification, you may visit this following site: Wilayah Indonesia, then please scroll your mouse to the bottom of the page (footer), you will see there are two languages, English and Indonesia.
  5. Another similar modification with that above, I also have created the custom language switcher in the sidebar of my theme. I put this following code in “footer.php” of my current theme:

    <?php do_action("icl_language_selector"); ?> // call the language selector

    after this following code:

    <!-- Right Sidebar -->
    <td id="right">

In order to display the output of the language switcher properly using the original css of WPML, then we have to put that css calling above. And don’t forget to always include the .css file that belongs to WPML plugin inside your header.php file in order to display the language selector properly.

Share

7,743 viewsPrint This Post Print This Post

Comments

  1. Thanks for this post.
    What is missing from the WPML site is basic instructions for the php noobs. The code you give in point 5 was all I required to put the drop down css language switcher into my site header, but I didnt find it on the wpml site. I spent a good three hours looking for this >:|

  2. Wilbur Whateley says:

    The information was qute useful to me, especially after reading the first post. Thanks.

  3. Hi, Masino. Thank you for your post. Could you please advise me: I need to put WPML language switcher (just a few flags without text; this is also a challenge as originally it does not allow remove text leaving just flags) into header and position them correctly (somwhere top right side).

    I was able to put this code in header: and it shows up, but I need to reposition them and would like to remove text (currently just made it same color as background, but it`s not very good as size of “switch is too big”. You can see site I`m working on here: http://bwh.silverbulletwebsolutions.com

    I`m total noob on coding. Please, please help me. Thank you in advance.

  4. meds says:

    kalau saya lebih suka menggunakan custom page (template), memisah single, archive theme untuk masing-masing kategori, dan custom page untuk halaman depan bahasa inggris. caranya bisa dengan membagi kategori menjadi id dan en, kemudian membuat sub category untuk masing2. contoh ada di web: paradesain.net

    web ini sungguh banyak tutorial berharga.

Speak Your Mind

*


*