I have the following in my functions in my child theme folder:
/* custom langauge menu item */
add_filter('wp_nav_menu_items','add_language_to_menu', 10, 2);
function add_language_to_menu( $items, $args ) {
global $polylang;
$items .= "<li class='menu-header-language'>" . pll_the_languages(array('dropdown'=>1, 'echo' => 0)) . "</li>";
foreach ($polylang->model->get_languages_list() as $language) {
$url = $force_home || ($url = $polylang->links->get_translation_url($language)) == null ? $polylang->links->get_home_url($language) : $url;
$urls[] = '"'.esc_js($language->slug).'":"'.esc_url($url).'"';
}
$urls = implode(',', $urls);
$js = "
<script type='text/javascript'>
//<![CDATA[
var urls = {{$urls}};
var d = document.getElementById('lang_choice');
d.onchange = function() {
for (var i in urls) {
if (this.value == i)
location.href = urls[i];
}
}
//]]>
</script>";
echo $js;
return $items;
}
So it’s based on the code from the widget. If I set the echo => 1 it will (of course) make two dropdowns in the select, where the echoed one works (onChange fires). I need the returned one (using echo => 0) to work. Any suggestions?