• Skip to main content
  • Skip to primary sidebar

WPDigs

You are here: Home / Coding / Adding a simple WPML language switcher to a menu bar

March 12, 2014 by Todd Leave a Comment

Adding a simple WPML language switcher to a menu bar

WPML by default has many multilingual switching options, but one thing has always been missing for those who just need a bilingual site and want to switch between two languages without dropdowns or other such default widgets.

 

This code does a few things you probably only want the last part.  You will need to know the name of the registered menu in this case it’s  header-menu

Add the code to you themes functions.php

If you are using the Genesis framework the two default menu names are primary and secondary

 

// Add the new menu
function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),
      'footer-menu' => __( 'Footer Menu' )

    )
  );
}
add_action( 'init', 'register_my_menus' );

//language switcher remove css
define('ICL_DONT_LOAD_NAVIGATION_CSS', true);
define('ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS', true);

/* WPML Main Menu */
function new_nav_menu_itemsmain($items,$args) {
    if (function_exists('icl_get_languages') && $args->theme_location == 'header-menu') {
        $languages = icl_get_languages('skip_missing=0');
        if(1 < count($languages)){
            foreach($languages as $l){
                if( ! $l['active'] ) {
                    $items = $items.'<li class="menu-item-'.$l['language_code'].'"><a href="'. $l['url'].'">'.$l['native_name'].'</a></li>';
                }
            }
        }
    }
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_itemsmain',10,2 );

The language switcher will be the last item on the menu and will now toggle between the two languages.

 

With some css you can move it over to the far right away from other menu items.

.menu-item-en, .menu-item-fr {float: right;}

 

switcher2 switcher1

Filed Under: Coding

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

updraft_plus


Multilingual WordPress

Get Beaver Builder Now!

WordPress form builder - formidable forms

Copyright © 2026 · Executive Pro on Genesis Framework · WordPress · Log in