Inline Language Switcher – Flags/Code/Code+Flags

Inline (horizontal) custom language selector with different options such as Flags only, Code only, Flags + Code with divisor.
Take the time to study the code.

To use call wpml_custom_language_selector(); in a template

function wpml_custom_language_selector(){
	// check if wpml is installed
	if (function_exists('icl_get_languages')) {
		// get active languages. Available params here: 
		// http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
		$languages = icl_get_languages('skip_missing=0&orderby=code&order=desc');			
		if(!empty($languages)){
			echo '<div class="lang_selector">';
				foreach($languages as $l){
				// from this point on, uncomment whatever output you like
					// flags only - do not display current language 
					/* if(!$l['active']){
						echo '<a href="'.$l['url'].'">
							<img src="'.$l['country_flag_url'].'" alt="'.$l['language_code'].'" />
						</a>';
					} */
					
				// the output code options below will display the current language
					// this adds an "active" class to the current language anchor
					$class = $l['active'] ? ' class="active"' : NULL;
					// code only
					/*$langs .=  '<a ' . $class . ' href="'.$l['url'].'">' . strtoupper ($l['language_code']). '</a> | ';*/
					//flag-code divided by a |
					/*$langs .=  '<a ' . $class . ' href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" alt="' . $l['language_code'] . '" />' . strtoupper ($l['language_code']). '</a> | ';*/
				}
				// strip the empty space and | from last language
				$langs = substr($langs,0,-3);
				echo $langs;
			echo '</div>';
		}
	}
}

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments