WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 12 months ago

#18232 new defect (bug)

wp_nav_menu - Setting walker parameter affects fallback_cb

Reported by: bryanjwatson Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.2.1
Component: Menus Keywords: has-patch needs-testing
Focuses: Cc:

Description

When using the walker parameter with wp_nav_menu and there is no menu present, wp_nav_menu falls back by default (set with fallback_cb) to wp_page_menu. It appears that the set walker also affects wp_page_menu, which in most cases breaks the output.

wp_nav_menu usage:

<?php wp_nav_menu(array('walker'=> new Walker_Nav_Menu)); ?>

Output when no menu is present (falling back on wp_page_menu):

// Formatted for readability
<div class="menu">
	<ul>
		<li id="menu-item-1" class="menu-item-1"><a></a></li>
		<li id="menu-item-2" class="menu-item-2"><a></a></li>
		<li id="menu-item-3" class="menu-item-3"><a></a></li>
	</ul>
</div>

Expected output:

// Formatted for readability
<div class="menu">
	<ul>
		<li class="page_item page-item-1"><a href="/first/" title="First">First</a></li>
		<li class="page_item page-item-2"><a href="/second/" title="Second">Second</a></li>
		<li class="page_item page-item-3"><a href="/third/" title="Third">Third</a></li>
	</ul>
</div>

As you can see, the output from wp_page_menu is getting filtered through the walker, but since the walker was designed for menus specifically, it renders the default output from wp_page_menu useless (empty tags..etc).

Attachments (2)

18232.patch (870 bytes) - added by SergeyBiryukov 3 years ago.
18232_v2.patch (1.5 KB) - added by bryanjwatson 3 years ago.

Download all attachments as: .zip

Change History (8)

SergeyBiryukov3 years ago

comment:1 SergeyBiryukov3 years ago

  • Keywords has-patch added; needs-patch removed

comment:2 bryanjwatson3 years ago

  • Keywords needs-testing added
  • Summary changed from wp_nav_menu - Setting walker parameter affects fallback_cb (wp_page_menu) to wp_nav_menu - Setting walker parameter affects fallback_cb

Simple patch, but unfortunately it does not account for the scenario when a function other than wp_page_menu is set as the fallback_cb parameter.

For example, this would still break:

<?php wp_nav_menu(array('fallback_cb'=>'wp_list_categories','walker'=>new Walker_Nav_Menu)); ?>

I believe the issue resides in the fact that the fallback function is inheriting the $args passed to wp_nav_menu.

Solution: Stop the $args inheritance on the fallback function, and create a new wp_nav_menu parameter to provide custom fallback args if needed. See patch 18232_v2.

bryanjwatson3 years ago

comment:3 markoheijnen2 years ago

  • Cc marko@… added

Is it really necessary to have special arguments for the fallback only for the walker? I rather would create a parameter only for setting the walker for the fallback.

comment:4 BenjaminRMueller2 years ago

  • Cc BenjaminRMueller added

comment:6 kwight12 months ago

  • Cc kwight@… added
Note: See TracTickets for help on using tickets.