Make WordPress Core

Opened 12 years ago

Last modified 5 years ago

#23902 new defect (bug)

Classes set with nav_menu_css_class are not accessible in walker_nav_menu_start_el

Reported by: jamesmehorter's profile jamesmehorter Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.1
Component: Menus Keywords: has-patch needs-testing needs-refresh
Focuses: Cc:

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 10 years ago.
Merging the CSS classes after the "nav_menu_css_class" filter back to $item->classes

Download all attachments as: .zip

Change History (7)

#1 @ericlewis
11 years ago

  • Keywords reporter-feedback added

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

@jamesmehorter can you provide a use case?

Version 0, edited 11 years ago by ericlewis (next)

#2 @jamesmehorter
11 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
10 years ago

  • Keywords needs-patch added; reporter-feedback removed

@tyxla
10 years ago

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

#4 @tyxla
10 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
10 years ago

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

#6 @chriscct7
9 years ago

  • Keywords needs-refresh added
Note: See TracTickets for help on using tickets.