Developers can create their own custom language switchers using the icl_get_languages API call from WPML. In this tutorial, you will find the full details of icl_get_languages and examples of custom language switchers built with it.

Quick overview:

  1. Call icl_get_languages to get the list of translations for the currently-displayed page
  2. Write your own PHP function that renders the language switcher
  3. Place your language switcher in your theme

Getting the list of (other) languages

Use icl_get_languages() to get a list of translations for any page. If you are using WPML 3.2 or above, you should consider using the similar hook filter wpml_active_languages.

Usage:

icl_get_languages('skip_missing=N&orderby=KEY&order=DIR&link_empty_to=str')

* N=0/1
* KEY=id/code/name/custom (name -> translated_name), (default: id)
* DIR=asc/desc (default: asc)
* link_empty_to = str (default: empty, works in conjunction with skip_missing=0 and allows using custom links for the languages that do not have translations for the current element. {%lang} can be used as placeholder for the language code)

Notes:

  • The skip_missing parameter tells the function how to treat languages with no translations.
  • The combination of orderby and order allows creating the drop-down language switcher or language switchers where languages always display in the same position.
  • The custom order can be defined in the WordPress admin under WPML » Languages » Language Switcher Options

The function returns an array with entries per language. For example, for a WordPress site running English, French and Italian, it will return this:

Array
(
 [en] => Array
 (
 [id] => 1
 [active] => 1
 [native_name] => English
 [missing] => 0
 [translated_name] => English
 [language_code] => en
 [country_flag_url] => http://yourdomain/wpmlpath/res/flags/en.png
 [url] => http://yourdomain/about
 )

 [fr] => Array
 (
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 

 
 
 ><
 
 
 
 
 
 
 
 
 
 
 
 
 ><
 
 
 



 
 
 



 



 
 
 



 
 



 

 
 
 
 
 
 
 
 
< >
 
 



 


 
 

 
 

 

 

 

 
 
 
 
&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;


 
 
&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;