WordPress.org

Make WordPress Core

Opened 13 months ago

Last modified 3 months ago

#24587 new defect (bug)

Walker_Page and Walker_Nav_Menu not compatible

Reported by: geardev Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Menus Keywords:
Focuses: Cc:

Description

Assume the following scenario: A developer decides to use a nav menu but wants to use a custom walker. He copy-pastes all the code in the Walker_Nav_Menu class and creates his own walker with it. But he doesn't assign a menu in the backend. Now this happens:

  • wp_nav_menu is called
  • The menu can't be found, therefore wp_page_menu is used
  • The Walker_Nav_Menu, that was passed to wp_nav_menu, is used to display the elements

But there are two problems.

The $args variable is converted to an object in wp-includes/nav-menu-template.php on line 145 by wp_nav_menu. Problem is, the wp_page_menu keeps the array instead and passes it to the nav menu walker, which needs the object. This will result in a few errors:

  • Notice: Trying to get property of non-object in wp-includes/nav-menu-template.php on line 88
  • Notice: Trying to get property of non-object in wp-includes/nav-menu-template.php on line 90
  • Notice: Trying to get property of non-object in wp-includes/nav-menu-template.php on line 90
  • Notice: Trying to get property of non-object in wp-includes/nav-menu-template.php on line 92

If you use this code snippet in an empty theme:

<?php wp_nav_menu( array(
        'theme_location' => 'non-existing-menu',
        'walker'         => new Walker_Nav_Menu
) );

Additionally, the WP_Post object is different. For a nav menu, the walker uses $item->title, which doesn't exist in the page walker. Therefore all <li>'s are empty.

One should either standardize the Walker system, or implement checks in the default Walker_Nav_Menu to provide a solid starting point for other developers.

Change History (3)

comment:2 ircbot3 months ago

This ticket was mentioned in IRC in #wordpress-dev by SergeyBiryukov. View the logs.

comment:3 SergeyBiryukov3 months ago

The type of $args in Walker_Nav_Menu::start_el() docs was changed from object to array in [25643]. However, it's treated as an object there, not as an array.

Note: See TracTickets for help on using tickets.