Skip to content

Instantly share code, notes, and snippets.

@anointed
Created July 17, 2012 06:38
Show Gist options
  • Select an option

  • Save anointed/3127660 to your computer and use it in GitHub Desktop.

Select an option

Save anointed/3127660 to your computer and use it in GitHub Desktop.
possible bug with wp_nav_menu()
This function is a proof of concept to show that the add_filter() is indeed able to work
and display the 'hello world' div above the opening ul.
<?php
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 should do the same thing, i.e. placing the hello world div above the opening ul. However there is a problem. It places the hello world after the wp_nav_menu ul.
<?php
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','' );
}
?>
@anointed

Copy link
Copy Markdown
Author

The concept:
I wanted to place a div before the wp_nav_menu() opening ul. This element is used as a 'clickable' button for mobile menu's as I am using some jQuery to swtich the menu upon a media query. In order for my script to work, the 'Menu' div needs to be placed before the opening ul.

The Problem:
The 'Menu' div is being placed AFTER the wp_nav_menu() ul instead of before it.

After working with Pippin most all day on this, he determined that it is probably a problem with the core wp_nav_menu() function. I wanted to create this gist to ask further questions and make sure that I am not doing it wrong, prior to creating a trac ticket.

Here is a hardcoded vs. of why I am doing this:
http://jsfiddle.net/wphotline/DqZgb/13/

Here is the wp_nav_menu() function above in action. If you view the source, you will see the 'Menu' div inserted after the ul
It is the main menu right below the site title. Shrink the browser window to see the issue.
http://bit.ly/KrXcGp

*please ignore the ugly theme, it's a work in progress and is nowhere near complete but it shows the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment