Opened 10 years ago
Closed 5 months ago
#32918 closed defect (bug) (duplicate)
_wp_menu_item_classes_by_context does not generate "current-menu-ancestor" class when the menu structure was changed by e.g. a plugin
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.2.2 |
| Component: | Menus | Keywords: | has-patch needs-refresh needs-test-info |
| Focuses: | Cc: |
Description
I am the developer of Category Posts in Custom Menu. My plugin extends Walker_Nav_Menu_Edit in order to change the menu structure. At the end of the edit loop, my plugin calls _wp_menu_item_classes_by_context on the resulting new menu structure.
In a particular case, this results in a missing "current-menu-ancestor" class, because _wp_menu_item_classes_by_context retrieves the ancestors from the database (i.e. the unchanged menu structure) instead of from the $menu_items array that was passed in and may now reflect a different structure.
Steps to reproduce with my plugin:
- Appearances > Menus
- Add to the menu a Page
- Add to the menu, as child of Page, a Category that has posts
- Check "Create submenu containing links to posts in this category."
This will result in my plugin editing the menu structure, and adding all posts to the menu instead of a link to the Category. Now navigate to one of these posts. You will see that the Page does not receive the "current-menu-ancestor" class.
I have performed a quick change in nav-menu-template.php that I have only tested with my plugin so far, so it will obviously need a rewrite. I'm attaching it nonetheless because it will quickly show in a few lines of code what took me a few paragraphs to explain here... :)
Attachments (1)
Change History (6)
#1
@
10 years ago
In the steps to reproduce, a last step is needed: Set "Remove original menu item" to "Never", to obtain a three-layered menu. (Otherwise, only 'current-menu-parent' would be applicable)
#2
@
10 years ago
- Keywords has-patch needs-testing added
Thanks for the patch, anaid! I'll review it shortly.
Related: #32367
#3
@
9 years ago
Updated patch added under #34839 where the unnecessary repetition of the code to handle menu item parent/ancestor has also been eliminated.
#4
@
5 months ago
- Keywords needs-refresh needs-test-info added; needs-testing removed
@anaid
A long time has passed, and I'm still able to reproduce this
Much has changed since then, even the new button seems to be Replace by posts in this category.
Currently, the patch is not applying, some variables have changed like $ancestor_id
Apart from this, I've tried making it apply, and it's partially working, but it's unable to select the right element any more. Needs some review. Adding for now a needs-refresh and see how it goes.
Still, I would also add some needs-test-info and see if this can be minimally reproducible in a simpler snippet of code than a whole plugin. Just to play around with this and maybe add some tests (not sure if this apply, but just in case)
#5
@
5 months ago
- Resolution set to duplicate
- Status changed from new to closed
Duplicate of #34839.
Found this other ticket, also commented in this one, but after further analysis I can observe that is essentially the same problem, but the other has more progress with a working patch, so I'm closing this for duplicate.
Patch to retrieve ancestors from the $menu_items array instead of from the database