[wp-trac] [WordPress Trac] #21302: possible bug with wp_nav_menu(), not allowing code to be wrapped around the output

WordPress Trac wp-trac at lists.automattic.com
Wed Jul 18 06:11:46 UTC 2012


#21302: possible bug with wp_nav_menu(), not allowing code to be wrapped around the
output
--------------------------+-----------------------------
 Reporter:  anointed      |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  3.0
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 I had a need to insert a div with a text link just prior to the
 wp_nav_menu() 'ul' output. So, following a tutorial I added a simple
 function that would 'wrap' the nav menu with beginning and ending
 variables. The problem I ran into is that no matter what I tried, the div
 with the text link 'Menu' would always show up after the menu 'ul' and not
 before.

 Code snippet:

 {{{
 function tumble_menu( $args = array() ) {
         /* Default arguments */
         $defaults = array(
                 'container'             => 'ul',
                 'menu_class'            => 'nav',
                 'menu_id'               => 'top_nav',
                 'theme_location'        => 'top-menu',
                 'echo'                  => true,
                 'before'                => '',
                 'after'                 => '',
                 'link_before'           => '',
                 'link_after'            => '',
                 'depth'                 => 1,
                 'sort_column'           => 'menu_order',
                 'show_container'        => false,
                 'walker'                => ''
                 );
         $defaults       = apply_filters( 'tumble_nav_default_args',
 $defaults);
         $args           = wp_parse_args( $args, $defaults );

         $main_menu = wp_nav_menu( $args );
 }
 function tumble_add_menu_wrapper($html, $begin, $end) {
         // wrap our original HTML with the new tags
         $html = $begin . $html . $end;
         return $html;
 }
 add_filter( 'tumble_menu_wrap', 'tumble_add_menu_wrapper', 10, 3 );

 function tumble_do_menu_wrapper() {
         $html = tumble_menu();
         echo apply_filters( 'tumble_menu_wrap', $html, 'div class="menu-
 button">Menu/div','' );
 }
 }}}
 *I had to modify the echo in the last function to remove the brackets on
 the div due to the editor stripping it out here.

 After spending most of the afternoon on this problem with Pippin, we
 determined that it is probably a bug with the core wp_nav_menu() function.

 Just in case it helps, here is the same function that does work as
 expected, only not using wp_nav_menu().

 snippet:

 {{{
 function pippin_sample_html() {
         ob_start(); ?>
         <ul>
                 <li>List Item <em>One</em></li>
                 <li>List <strong>Item</strong> Two</li>
                 <li>List Item <a href="#">Three</a></li>
         </ul>
         <?php
         return ob_get_clean();
 }


 function pippin_add_html_wrapper($html, $begin, $end) {
         // wrap our original HTML with the new tags
         $html = $begin . $html . $end;
         return $html;
 }
 add_filter('pippin_html_wrap', 'pippin_add_html_wrapper', 10, 3);

 function pippin_print_html() {
         $html = pippin_sample_html();
         echo apply_filters('pippin_html_wrap', $html, 'div
 id="sample_wrapper">hello world/div', '');
 }}}

 This function, 'almost identical to the first one', does indeed place the
 'hello world' prior to the opening 'ul'.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21302>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list