Opened 6 months ago
Last modified 6 weeks ago
#65348 new defect (bug)
Adding menu items in bulk, checked items forgotten when navigating
| Reported by: | daveykj | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Menus | Version: | 6.9.4 |
| Severity: | minor | Keywords: | has-patch has-unit-tests needs-testing |
| Cc: | Focuses: | ui, javascript, administration |
Description
When you want to add multiple menu-items to a menu (through /wp-admin/nav-menus.php) using the checkboxes, it doesn't save the checked items when using pagination to navigate through multiple 'pages' of items. Especially when using the 'Show all' option.
For example; there are 6 'pages' of items. You select 3 items on page 1, you navigate to page 3, select also 3 items here, the items on page 1 are 'forgotten'.
Attachments (2)
Change History (12)
This ticket was mentioned in PR #11977 on WordPress/wordpress-develop by @ekamran.
6 months ago
#2
- Keywords has-patch added
Trac ticket: https://core.trac.wordpress.org/ticket/65348
## Summary
This fixes an issue in the Menus screen where selected menu items are lost when moving between paginated meta box pages before clicking "Add to Menu".
The patch stores selected menu item data per meta box before AJAX pagination replaces the checklist markup, restores checked state when returning to a page, and clears stored selections when switching tabs or after adding items.
## Testing
Tested locally:
- Pages: selected items on page 1 and page 3, then added them together.
- Pages: returned to page 1, verified checks were restored, unchecked one item, and confirmed it was not added.
- Select All: selected current page items, paginated, selected another item, and added all expected items.
- Categories: selected items across taxonomy pagination and added all expected items.
- Tab switching: confirmed cached selections are cleared.
- Custom Links: confirmed custom links still add normally.
- Real AJAX add-menu-item request: confirmed selected items from multiple pages are added.
- Ran
npm run grunt -- jshint:core --file=src/js/_enqueues/lib/nav-menu.js. - Ran
git diff --check.
Also checked compatibility with the 6.9.4 tag:
- Patch applies cleanly to
6.9.4. jshint:corepasses on the patched6.9.4file.- Same browser regression checks passed using the patched
6.9.4nav-menu.js.
#3
@
6 months ago
Tested @ekamran's patch on PR 11977 and can confirm it addresses the reported issue.
I was able to reproduce the issue on trunk: when selecting menu items in a paginated nav menu metabox, then navigating to another page of that metabox via AJAX pagination, the previous selections are lost because the metabox HTML is replaced and addSelectedToMenu() only reads currently checked inputs from the active panel.
With @ekamran's patch applied, the targeted JS lint passes:
npm run grunt -- jshint:core --file=src/js/_enqueues/lib/nav-menu.js
Result: 1 file lint free.
I also verified the changed behavior with a focused DOM regression check:
- selections from page 1 are cached before the metabox markup is replaced
- selections from a later page are combined with the cached selections
- returning to a previously selected page restores the checked state
- unchecking a restored item removes it from the cached selections
- after adding items to the menu, the cache is cleared
This looks like the right fix for the state-loss issue without changing the existing AJAX pagination flow.
#4
@
3 months ago
- Keywords has-unit-tests added
Added QUnit coverage for the patch in PR 11977, in tests/qunit/wp-admin/js/nav-menu.js. It fires real clicks through the pagination, select all, tab switch and add to menu paths, so the wiring is covered and not just the helper functions.
Writing the tests surfaced one case the patch missed. Rows that do not map to a real object, such as Home and post type archives, are rendered with the negative $_nav_menu_placeholder as their object ID. That counter restarts on every request, so those rows returned under a different ID and lost their selection. A default install has no static front page, so the Home row is present for most people testing this.
That is a separate commit so it can be dropped if it should be handled in its own ticket.
Adding has-unit-tests. This is ready for review.
@ekamran commented on PR #11977:
3 months ago
#5
Added QUnit coverage for this patch, in tests/qunit/wp-admin/js/nav-menu.js. It exercises the pagination, select all, tab switch and add to menu paths by firing real clicks, so the wiring is covered and not just the helpers.
While writing it I found the fix missed one case. Rows that do not map to a real object, such as Home and post type archives, get the negative $_nav_menu_placeholder as their object ID. That counter restarts every request, so those rows came back under a different ID and lost their tick. On a default install with no static front page the Home row is always present, so this was easy to hit.
That is a separate commit (e76e2f36) so it can be dropped if you would rather handle it in its own ticket.
#6
@
3 months ago
Still reproduces on 7.1, unchanged since the 6.9.4 report.
Retested on a clean 7.1 install with Twenty Seventeen and 61 pages so the Pages metabox paginates. Selected two items on page 1, moved to page 2, returned to page 1: both selections were gone. With the patch from PR 11977 applied the selections survived the round trip, and "Add to Menu" added the items chosen on both pages.
One detail worth recording about the placeholder rows. Home is rendered on every page of the metabox, not only the first, and it carries the negative $_nav_menu_placeholder ID that restarts on each request. So a default install with no static front page hits that case as soon as anyone paginates.
Also verified while testing: selections in the Pages, Posts and Categories metaboxes stay separate, and "Add to Menu" in one metabox adds only its own items. The Search tab returns a capped result set with no pagination, so there is no pagination path there to handle.
This ticket was mentioned in Slack in #core by kamran_abdul_aziz. View the logs.
2 months ago
@ekamran commented on PR #11977:
2 months ago
#9
@audrasjb @joedolson @westonruter — could one of you take a look?
Menu item selections are lost when paginating a metabox before clicking
"Add to Menu". Patch caches and restores them; QUnit coverage added.
Still reproduces on 7.1. @yusufmudagal verified it on the ticket.
#10
@
6 weeks ago
Patch Testing Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/11977
Environment
- WordPress: 7.1.2 (before) / trunk with PR #11977 (after), both in WordPress Playground
- Browser: Google Chrome 153.0.8010.52
- OS: Linux
- Theme: Twenty Seventeen
- MU Plugins: None
- Plugins: None
Steps taken
- Created 110 published pages and an empty menu named "Test Menu".
- Went to Appearance > Menus and opened the Pages > View All tab.
- Checked 3 pages on page 1 of the checklist.
- Used the pagination links to go to page 3 and checked 3 more pages.
- Clicked "Add to Menu".
- Repeated the same steps with the patch applied.
- ✅ Patch is solving the problem.
Results
- Before the patch: only the 3 pages checked on page 3 were added to the menu. The selections from page 1 were lost.
- With the patch: all 6 checked pages (3 from page 1 and 3 from page 3) were added to the menu.
Expected result
Items checked on different pages of the Pages checklist stay selected, and all of them are added when clicking "Add to Menu".
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)


I was able to reproduce this locally on trunk.
Testing steps:
Result: only the page 3 items were added to the menu. The page 1 selections were lost when navigating between pages in the menu item selector.
So I can confirm the issue is reproducible.