Skip to content

Instantly share code, notes, and snippets.

@anointed
Created July 16, 2012 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anointed/3120416 to your computer and use it in GitHub Desktop.
Save anointed/3120416 to your computer and use it in GitHub Desktop.
Filter to apply a div before menu output
Here is what I ended up going with. Not the way I wanted to do it, but hell it works...
<?
function tumble_menu( $args = array() ) {
/* Default arguments */
$defaults = array(
'container' => 'ul',
'menu_class' => 'nav',
'menu_id' => 'main_menu',
'theme_location' => 'main-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 );
echo '<div class="menu-button">Menu</div>';
$main_menu = wp_nav_menu( $args );
}
<?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>', '');
?>
<?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',
'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>','' );
}
?>
@pippinsplugins
Copy link

I just explored the core source and it turns out that there is actually a filter already applied to the menu that you could use:

$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );

Line 232: http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/nav-menu-template.php#L0

@anointed
Copy link
Author

Thanks Pippin. That should do what I need it to do.

I'm going to make it a point to try and learn why the wrapper function didn't work as I had it. Basically I don't understand is why it matters.

I mean I am using a 'wrapper' function to insert stuff before and after (should I choose to) an object. Why the type of object makes a difference is rather confusing.

Your example snippet uses an unordered list and it can be 'wrapped'

My example snippet uses an unordered list and it can't be wrapped.

Something is going on either with WP, or php that I just don't yet grasp. My little brain says it should just work.

Would make a good follow up article someday on your tutorial as it's targeted at people trying to learn more about php and add_filter, add_action, etc... I can guarantee that other people are going to run into this issue and have the same questions.

Thanks so much for digging through this with me today.

@pippinsplugins
Copy link

The things that's strange is that your example should work. There really isn't anything wrong with it that I can see. I think it's a WP core issue with wp_nav_menu.

@anointed
Copy link
Author

I posted a new gist on top of what I ended up going with. Not the 'right' way, but oh well. I need to learn more about apply_filters() to be able to do it the right way.

Sometime I'll post a trac ticket about the possible issue with wp_nav_menu().

Suppose it's kinda cool that I actually found a core issue... first time for me. Usually I'm just swatted down and told I'm doing it wrong. Nice to hear the opposite for a change.

@pippinsplugins
Copy link

If you end up posting a trac ticket, let me know as I'd like to follow it.

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