Title: Problem using output buffer within walker
Last modified: April 25, 2017

---

# Problem using output buffer within walker

 *  [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/)
 * (@scorpiotiger)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/problem-using-output-buffer-within-walker/)
 * I’m trying to use the output buffer to get the content for dynamic_sidebar() 
   from within a menu walker. The result I just doesn’t place the content within
   the expected `<div>, but instead places it outside the <li> for the menu item.
   I just can’t make sense of it. If I perform the same buffering outside the start_el
   function, then I get the expected result, though of course not where I need it.
   Is there something going on here that I’m not aware of?
 *     ```
       class Menu_With_Description extends Walker_Nav_Menu {
       	function start_el( &$output, $item, $depth = 0, $args = array(), $current_object_id = 0) {
       		global $wp_query;
       		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
   
       		$class_names = $value = '';
   
       		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
   
       		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
       		$class_names = ' class="' . esc_attr( $class_names ) . ' menu-level-' . $depth . '"';
   
       		$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
   
       		$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
       		$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
       		$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
       		$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
   
       		$item_output = $args->before;
       		$item_output .= '<a'. $attributes .'>';
       		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
       		$item_output .= '</a>';
       		//$item_output .= '<span class="menu-description">' . $item->description . '</span>';
   
       		if ( is_active_sidebar( 'mega-menu-widget-area-' . $item->ID ) ) {
       			$item_output .= "<div id=\"mega-menu-{$item->ID}\" class=\"mega-menu\">";
       			ob_start();
       			dynamic_sidebar( 'mega-menu-widget-area-' . $item->ID );
       			$item_output .= ob_get_clean();
       			$item_output .= "</div>";
       		}
   
       		$item_output .= $args->after;
   
       		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
       	}
       }
       ```
   
 * _[Moderator note: code fixed. Please wrap code in the backtick character or [use the code button](https://codex.wordpress.org/Forum_Welcome#Posting_Code).]_
    -  This topic was modified 9 years, 1 month ago by [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/).
    -  This topic was modified 9 years, 1 month ago by [bdbrown](https://wordpress.org/support/users/bdbrown/).
    -  This topic was modified 9 years, 1 month ago by [bdbrown](https://wordpress.org/support/users/bdbrown/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/)
 * (@scorpiotiger)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/problem-using-output-buffer-within-walker/#post-9063784)
 * Sorry, wrong code. Corrected above.
    -  This reply was modified 9 years, 1 month ago by [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/).
 *  Thread Starter [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/)
 * (@scorpiotiger)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/problem-using-output-buffer-within-walker/#post-9064903)
 * Ok; looks like I’ve gotten to the bottom of it. For those who are trying to render
   a widget within a menu (or potentially anywhere); the dynamic_sidebar() function
   opens a new `<li>` without opening an `<ul>`. This is causing strange results
   in the output as the browser attempts to add implied closing of tags.
    -  This reply was modified 9 years, 1 month ago by [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problem using output buffer within walker’ is closed to new replies.

 * 2 replies
 * 1 participant
 * Last reply from: [scorpiotiger](https://wordpress.org/support/users/scorpiotiger/)
 * Last activity: [9 years, 1 month ago](https://wordpress.org/support/topic/problem-using-output-buffer-within-walker/#post-9064903)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
