WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently trying to add a some links to the end of my wp_nav_menu but am having a hard time with the callback function that creates the list of languages I want to insert. The first function is from a WPML plugin and works correctly anywhere else.

function lang_selector_links(){


    if(!empty ($languages)){

        foreach ($languages as $language){
            echo '<li><a href="' . $language['url'] . '" title="' . $language['translated_name'] . '"<img src="' . $language['country_flag_url'] . ' alt="' .$language['language_code'] . '"/>'. $language['language_code']. '</a></li>';

    } else {
        echo 'no languages';

function my_lang_links( $items, $args ) {
        $lang_list= lang_selector_links();

        $items .= '<li>' . $lang_list . '</li>';

    return $items;
add_filter('wp_nav_menu_items','my_lang_links', 10, 2);

If I call lang_selector_links()in any other context it works as expected and outputs a list of supported languages but I can't seem to call it within the filter.

Edit: It turns out that it is outputting them only I was expecting the links to be part of the unordered list that contains the other menu items. Instead it adds them before.

share|improve this question
Do you have debugging information? Also, if the problem is that a third party plugin function doesn't work, this may be off topic. – s_ha_dum Mar 31 '14 at 12:57
No the third party function works fine. I've used it outside of wp-menu without problems. I'm not getting any errors just an empty <li></li>. – mantis Mar 31 '14 at 16:06
This is a PHP issue. Your lang_selector_links function echoes output, and you try to assign that to a variable in my_lang_links function. That will only work if your lang_selector_links function returns its value. – Milo Mar 31 '14 at 18:56
Yes, you're right. It works correctly if I return a value rather than echoing it. Of course if I simply change echo to return I'll only get one value so I suppose I need to push them into an array? – mantis Mar 31 '14 at 19:04
you could use output buffering. – Milo Mar 31 '14 at 19:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.