Created
November 27, 2011 17:51
-
-
Save backflip/1397895 to your computer and use it in GitHub Desktop.
WPML language switcher: Transform names to language codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WPML language switcher: Transform names to language codes | |
* | |
* The language switcher is added to the theme by setting | |
* 'Den Sprachumschalter im WP-Menü anzeigen' to 'Meta right'. | |
* However, there is no option to display the language code only. | |
* Instead, the whole name is displayed (e.g., "Deutsch"). | |
* As a work-around we filter all items of this navigation and | |
* replace the full names with the corresponding codes. | |
* | |
* This could have been solved with a custom language switcher as well, | |
* but it seemed to be more intuitive like this. | |
*/ | |
function sbb_filter_language_nav($items, $args){ | |
$args = (array)$args; | |
$theme_location = 'meta_right'; | |
if ($args['theme_location'] == $theme_location && function_exists('icl_get_languages')) { | |
$langs = icl_get_languages('skip_missing=0'); | |
$map = array(); | |
foreach ($langs as $code => $lang) { | |
// Very rough, should see some regex love... | |
$replace = array($lang['native_name'], $lang['translated_name']); | |
$items = str_replace($replace, $code, $items); | |
} | |
} | |
return $items; | |
} | |
add_filter('wp_nav_menu_items', 'sbb_filter_language_nav', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment