Make WordPress Core

Opened 14 years ago

Last modified 10 days ago

#23902 new defect (bug)

Classes set with nav_menu_css_class are not accessible in walker_nav_menu_start_el

Reported by: jamesmehorter Owned by:
Priority: normal Milestone:
Component: Menus Version: 3.5.1
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Hello,

If you use the filter hook nav_menu_css_class to add classes to a menu item, those classes are not available in other menu filter hooks like walker_nav_menu_start_el. This can be found in the start_el function in wp-includes/nav-menu-template.php. I would think line 75:

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );


Should merge those $class_names back into $item.

Thanks

Attachments (1)

nav-menu-template.php.patch​ (746 bytes ) - added by tyxla 12 years ago.
Merging the CSS classes after the "nav_menu_css_class" filter back to $item->classes

Download all attachments as: .zip

Change History (10)

#1 @ericlewis
13 years ago

  • Keywords reporter-feedback added

I don't see a utility in having the class names of the <li> wrapper in the args for the inner output's filter (walker_nav_menu_start_el).

@jamesmehorter can you provide a use case?

Last edited 13 years ago by ericlewis (previous) (diff)

#2 @jamesmehorter
13 years ago

Use case:

I needed to add a little <div></div> inside menu <li>'s with the classes current-page-ancestor or current_page_parent.

If I added either of those classes manually via nav_menu_css_class, I was then unable to access those classes in the later-run walker_nav_menu_start_el hook to add the html.

(When using a page as a CPT ancestor you have to manually add the classes as mentioned above)

@ericlewis - regardless of use, a later-run hook should always have the most current object-whatever it is. Given that nav_menu_css_class runs before walker_nav_menu_start_el - any change made to the menu via nav_menu_css_class should be accessible in any later hook.

Does that help?

#3 @jamesmehorter
13 years ago

  • Keywords needs-patch added; reporter-feedback removed

@tyxla
12 years ago

Merging the CSS classes after the "nav_menu_css_class" filter back to $item->classes

#4 @tyxla
12 years ago

I just found myself in a similar situation to the use case that @jamesmehorter described above.
I've just attached a small patch that resolves this one.

#5 @tyxla
12 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

#6 @chriscct7
11 years ago

  • Keywords needs-refresh added

#7 @r1k0
9 months ago

  • Keywords needs-testing removed

The patch is quite ancient and fails to apply with the following error:

patching file src/wp-includes/nav-menu-template.php
Hunk #1 FAILED at 95.
1 out of 1 hunk FAILED -- saving rejects to file src/wp-includes/nav-menu-template.php.rej

Testing would not be necessary at this moment until patch is refreshed. Removed needs-testing

This ticket was mentioned in ​PR #15243 on ​WordPress/wordpress-develop by ​@abhishekfdd.


10 days ago
#8

  • Keywords has-unit-tests added; needs-refresh removed

Refreshes the approach from the 2015 patch on the ticket against current trunk, where the code now lives in Walker_Nav_Menu::start_el().

The array returned by nav_menu_css_class was only imploded into the <li> class string and then discarded, so $menu_item->classes still held the unfiltered classes when walker_nav_menu_start_el (and nav_menu_link_attributes, nav_menu_item_title, etc.) ran. This stores the filtered array back on the menu item. Rendered markup is unchanged.

Why not leave it to userland? A callback can already do $menu_item->classes = $classes; itself, but that only works if every plugin touching classes does it, and the last filter wins. Core is the one place that can guarantee later hooks see what was actually rendered.

Behavior change to note: $menu_item->classes will now include menu-item-{ID}, which start_el() appends before the filter runs. That was left in on purpose so the object matches the rendered <li> exactly. If reviewers would prefer to strip it before writing back, that's a one-line change.

Adds a unit test covering an added class, a removed class, an untouched class, and the rendered output.

#9 @abhishekfdd
10 days ago

Refreshed patch against current trunk: ​https://github.com/WordPress/wordpress-develop/pull/15243

The code now lives in Walker_Nav_Menu::start_el(). The PR stores the result of nav_menu_css_class back on $menu_item->classes so later hooks (walker_nav_menu_start_el, nav_menu_link_attributes, nav_menu_item_title) see what was actually rendered. Markup is unchanged. Unit test included, and it fails without the fix.

Two points worth review:

  • A callback can already do this itself, but only reliably if it's the last filter to run. Doing it in Core guarantees consistency for every later hook.
  • The written-back array includes menu-item-{ID}, which start_el() appends before the filter. I left that in so the object matches the <li> exactly. Happy to strip it if that's preferred.
Note: See TracTickets for help on using tickets.