Opened 16 years ago
Closed 11 years ago
#16594 closed enhancement (fixed)
inconsistency in wp_nav_menu_items
| Reported by: | thomask | Owned by: | chriscct7 |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.3 |
| Component: | Menus | Version: | 3.0 |
| Severity: | normal | Keywords: | has-patch commit |
| Cc: | Focuses: |
Description
Not sure if it is a bug: when I create a menu, e.g.
wp_nav_menu(array( 'theme_location' => 'footer' ));
and put there a menu (E.g. named "My menu") using nav-menu.php editor and then try to call wp_nav_menu_items filter to see all arguments, e.g.
add_filter('wp_nav_menu_items','my_test', 10, 2);
function my_test($items, $args) {
print_r ($args);
}
the resulting object is
(
[menu] =>
[container] => div
[container_class] =>
[container_id] =>
[menu_class] => menu
[menu_id] =>
[echo] => 1
[fallback_cb] =>
[before] =>
[after] =>
[link_before] =>
[link_after] =>
[items_wrap] => <ul id="%1$s" class="%2$s">%3$s</ul>
[depth] => 0
[walker] =>
[theme_location] => menu
)
But if I put the same "My menu" to the same place using widget, I got object with extended 'menu'
[menu] => stdClass Object
(
[term_id] => 4
[name] => My menu
[slug] => my-menu
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 3
)
[container] => div
[container_class] =>
[container_id] =>
[menu_class] => menu
[menu_id] =>
[echo] => 1
[fallback_cb] =>
[before] =>
[after] =>
[link_before] =>
[link_after] =>
[items_wrap] => <ul id="%1$s" class="%2$s">%3$s</ul>
[depth] => 0
[walker] =>
[theme_location] =>
I guess that they should be the same (except 'theme_location') - the problem is, that using the first scenario, i got no way to find "My menu" details - id, name, slug ... of the menu, so i even cannot filter by them in my functions (e.g. apply filter just for a specific menu).
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
I think it is the expected behavior. For wp_nav_menu we pass 'theme_location' but in the widget we pass 'menu' object.
If you want to get the menu object in wp_nav_menu like in widget, you can try this
add_filter('wp_nav_menu_items','my_test', 10, 2); function my_test($items, $args) { if( !$args->menu ) { $locations = get_nav_menu_locations(); print_r( wp_get_nav_menu_object( $locations[$args->theme_location] ) ); } else{ print_r ( $args->menu ); } }If it is not the expected behavior then adding
$args->menu = $menu;in wp_nav_menu function in wp-includes/nav-menu-tempalte.php fixes it.