Support » Plugins » 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.]

    • This topic was modified 4 years, 5 months ago by scorpiotiger.
    • This topic was modified 4 years, 5 months ago by bdbrown.
    • This topic was modified 4 years, 5 months ago by bdbrown.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter scorpiotiger

    (@scorpiotiger)

    Sorry, wrong code. Corrected above.

    • This reply was modified 4 years, 5 months ago by scorpiotiger.
    Thread Starter scorpiotiger

    (@scorpiotiger)

    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 4 years, 5 months ago by scorpiotiger.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem using output buffer within walker’ is closed to new replies.