do_action appearing outside of menu?

I’m using Bill Erickson’s “Add Extra Code to Any Menu” function:

<?php
/**
 * Add Extra Code to Any Menu
 * @author Bill Erickson
 * @link http://www.billerickson.net/customizing-wordpress-menus/
 *
 * @param string $menu
 * @param object $args
 * @return string modified menu
 */
function be_menu_extras($menu, $args) {
//$extras = '<li class="right">Your extras go here</li>';
if ('secondary' == $args->theme_location) 
    //return $menu . $extras;
      return $menu . '<li>' . do_action('posts_logout_link') . '</li>';
else
    return $menu;
}
add_filter('wp_nav_menu_items','be_menu_extras', 10, 2);

to insert do_action('posts_logout_link'); from the Logout Password Protected Posts plugin.

Read More

Can anyone shed some light as to why do_action appears outside the $menu.

If I use html, it appears at the end on the menu wrapped in the <li> tags, as it’s supposed to. (Using $extras seemed to duplicate the link in other areas.)

Related posts

Leave a Reply

1 comment

  1. I think the reason is echo vs return:

    The function posts_logout_link from

    http://plugins.svn.wordpress.org/logout-password-protected-posts/trunk/logout.php

    is using echo.

    Your code is therefore both returning and echoing values.

    ps: You could consider using output buffering to fix this

    ob_start();
    do_action('posts_logout_link');
    $logout_link=ob_get_clean();
    
    return $menu . '<li>' . $logout_link . '</li>';
    

    or modify the plugin code functions to your needs and place them into your functions.php file.