Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(){

$languages=icl_get_languages('skip_missing=0&orderby=code');

    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 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 at 16:06
1  
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 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 at 19:04
    
you could use output buffering. –  Milo Mar 31 at 19:10

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.