Opened 13 years ago
Last modified 6 weeks ago
#23902 new defect (bug)
Classes set with nav_menu_css_class are not accessible in walker_nav_menu_start_el
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 3.5.1 |
| Component: | Menus | Keywords: | has-patch 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)
Change History (8)
#2
@
12 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?
#4
@
11 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.
#7
@
6 weeks 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
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?