WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 2 years ago

#16594 closed enhancement (fixed)

inconsistency in wp_nav_menu_items

Reported by: thomask Owned by: chriscct7
Milestone: 4.3 Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords: has-patch commit
Focuses: Cc:

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).

Attachments (2)

16594.diff (536 bytes) - added by greuben 7 years ago.
16594.1.patch (399 bytes) - added by chriscct7 3 years ago.

Download all attachments as: .zip

Change History (15)

#1 @greuben
7 years ago

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.

#2 @nacin
7 years ago

  • Keywords needs-patch added
  • Type changed from defect (bug) to enhancement
  • Version changed from 3.1 to 3.0

If it is not the expected behavior then adding $args->menu = $menu; in wp_nav_menu function in wp-includes/nav-menu-template.php fixes it.

That sounds like a good enhancement.

#3 @nacin
7 years ago

  • Milestone changed from Awaiting Review to Future Release

@greuben
7 years ago

#4 @greuben
7 years ago

  • Keywords has-patch added; needs-patch removed

#5 @billerickson
6 years ago

  • Cc bill.erickson@… added

#6 @goto10
6 years ago

  • Cc dromsey@… added

#7 @kraftbj
5 years ago

  • Cc bk@… added

@chriscct7
3 years ago

#9 @chriscct7
3 years ago

  • Milestone changed from Future Release to 4.3

#10 @chriscct7
3 years ago

  • Owner set to chriscct7
  • Status changed from new to assigned

#11 @chriscct7
3 years ago

  • Keywords commit added

#12 @chriscct7
3 years ago

  • Status changed from assigned to accepted

#13 @wonderboymusic
2 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 32803:

In wp_nav_menu(), ensure that the $menu arg is populated when passed to filters.

Props greuben, chriscct7.
Fixes #16594.

Note: See TracTickets for help on using tickets.