Skip to content

Abilities API: add core/read-nav-menus ability - #858

Closed
Builder106 wants to merge 11 commits into
WordPress:developfrom
SankofaForge:add/nav-menu-core-abilities
Closed

Builder106 wants to merge 11 commits into
WordPress:developfrom
SankofaForge:add/nav-menu-core-abilities

Conversation

@Builder106

@Builder106 Builder106 commented Jul 11, 2026 •

Copy link
Copy Markdown

What?

See WordPress/mcp-adapter#181

Adds a read-only core/read-nav-menus ability: fetch a single nav menu (with its items) by ID, slug, or registered theme location, or list every menu on the site alongside the registered theme locations and their current assignments.

Why?

An end-to-end agent session against a live WordPress site surfaced a gap: there's no way for an agent to list, inspect, or otherwise reason about the site's navigation menus. wp_get_nav_menus(), wp_get_nav_menu_items(), get_registered_nav_menus(), and get_nav_menu_locations() are all stable core functions, but none of them are exposed through the Abilities API. Full details and the manual-work-cost ranking are in WordPress/mcp-adapter#181.

This is proposed as a core/* ability, following the same path as core/read-users and core/read-settings: land here first for review and real-usage feedback, then a WordPress core Trac ticket and eventual merge proposal. Core Trac ticket: https://core.trac.wordpress.org/ticket/65612

How?

core/read-nav-menus is one ability with a oneOf input schema, rather than split list/get abilities, matching the shape of the existing Users and Settings ability classes:

  • id, slug, or location: returns a single menu, with its items, resolved via wp_get_nav_menu_object() and wp_get_nav_menu_items(). location resolves through get_nav_menu_locations() and errors if nothing is assigned there.
  • no input, optionally with search: returns every menu (without items) plus the site's registered theme locations (get_registered_nav_menus()) and which menu is currently assigned to each (get_nav_menu_locations()).

Since core does not yet register a navigation ability category, the class registers one itself on wp_abilities_api_categories_init, the same way Main.php registers the ai category, so it's easy to drop once core has its own. Like Users and Settings, it unregisters any core-provided core/read-nav-menus first so the plugin's version always wins, and hooks on wp_abilities_api_init at priority 11 to run after core's own registration.

Permission is gated behind edit_theme_options, the same capability WordPress requires to manage menus in wp-admin. Nav menus are ordinarily public (they're what renders in the site header), but a menu not assigned to any location, or the full location-assignment map, exposes more of the site's structure than the front end does, so I went with the more conservative gate here. Happy to loosen this if reviewers think read access should be broader.

Nav menu items come back from wp_get_nav_menu_items() as WP_Post objects, but most of the fields used here (title, url, classes, etc.) are dynamic properties wp_setup_nav_menu_item() adds at runtime, not part of WP_Post's declared property list. format_menu_item() reads them via get_object_vars() rather than direct property access so static analysis doesn't flag them as undefined.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Writing of integration tests. Reviewed by me before this PR was opened.

Testing Instructions

  1. Check out this branch.

  2. Run the plugin's PHPUnit integration suite, or just tests/Integration/Includes/Abilities/Nav_Menus/Nav_MenusTest.php. Note: this hadn't been run in CI as of opening the PR; PHPCS and PHPStan have since been fixed and are passing.

  3. Manually: register a nav menu with at least one item, assign it to a theme location, then call core/read-nav-menus:

    • with no input, to get the collection
    • with id set to the menu's term ID
    • with slug set to the menu's slug
    • with location set to the theme location slug

    Check the response shapes match the output schema in each case.

Changelog Entry

Added - Read-only core/read-nav-menus ability for fetching nav menus and their items.

Open WordPress Playground Preview Open WordPress Playground Preview

@Builder106
Builder106 requested a review from a team July 11, 2026 16:56
@github-actions

github-actions Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Builder106 <builder106@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@codecov

codecov Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.66216% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 85.72%. Comparing base (6dbbd48) to head (28cb093).

Files with missing lines Patch % Lines
includes/Abilities/Nav_Menus/Nav_Menus.php 99.65% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             develop     #858      +/-   ##
=============================================
+ Coverage      85.49%   85.72%   +0.23%     
- Complexity      4807     4861      +54     
=============================================
  Files            188      190       +2     
  Lines          17734    18030     +296     
=============================================
+ Hits           15161    15456     +295     
- Misses          2573     2574       +1     
Flag Coverage Δ
unit 85.72% <99.66%> (+0.23%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jeffpaul jeffpaul added this to the 1.3.0 milestone Jul 13, 2026
@jeffpaul jeffpaul moved this from Triage to Needs review in WordPress AI Roadmap Jul 13, 2026
Registers a read-only core/read-nav-menus ability that retrieves a
single nav menu (with items) by ID, slug, or registered theme
location, or the full collection of menus alongside registered theme
locations and their current assignments. Follows the shape of the
existing core/read-users and core/read-settings abilities: a single
ability per resource with a oneOf input schema, rather than split
list/get abilities.

Also registers a navigation ability category, since core does not yet
provide one.

See WordPress/mcp-adapter#181 and the core.trac.wordpress.org ticket
proposing this for core.
Leftover from adapting the Users ability's pattern; the collection
branch is reached by falling through the id/slug/location checks
rather than a dedicated lookup-type helper, so the constant was never
read.
- Fully qualify \WP_Error, \WP_Term, \WP_Post in docblock annotations
  (SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation)
- Invert the two nav-menu/location loops to early-continue instead of
  nesting the append inside the if (SlevomatCodingStandard.ControlStructures.EarlyExit)
- Align consecutive assignment operators in format_menu()
  (Generic.Formatting.MultipleStatementAlignment)
- Rename format_menu_item()'s closure param off the reserved word `class`
- Read nav menu item's dynamic properties (added by wp_setup_nav_menu_item(),
  not part of WP_Post's real property list) via get_object_vars() instead of
  direct property access, since PHPStan has no way to know they exist on a
  plain WP_Post
@Builder106
Builder106 force-pushed the add/nav-menu-core-abilities branch from 5b28f3e to 145b33f Compare July 18, 2026 22:51
@Builder106

Copy link
Copy Markdown
Author

@gziolo @jorgefilipecosta Just a quick heads up: I've pushed a few updates to fix the initial PHPCS and PHPStan CI failures I mentioned in the PR description, and cleaned up an unused constant. All CI checks are green now, so this is ready for review whenever you have a chance.

@jeffpaul jeffpaul mentioned this pull request Aug 10, 2026
48 tasks done
@dkotter dkotter modified the milestones: 1.3.0, 1.4.0 Aug 12, 2026
@gziolo
gziolo removed their request for review September 3, 2026 09:41
@jeffpaul jeffpaul modified the milestones: 1.4.0, 1.5.0 Sep 17, 2026
Builder106 and others added 5 commits September 25, 2026 09:14
Place Nav_Menus under Gated_Abilities so it respects the Custom
Abilities experiment gate, and remove redundant registrations from
Main::init() that triggered duplicate-registration notices during
ability lookup. Ensure the navigation category is initialized in test
fixtures, and update gated ability count assertions.

Ref WordPress#858
Add tests for schema-valid object inputs, unassigned theme location
filtering, non-term items returned by collection hooks, and location
mapping when locations point to different menus.

This brings patch test coverage on Nav_Menus to 100%.
@jeffpaul

Copy link
Copy Markdown
Member

@Builder106 are you able to update from the review comments here?

@Builder106

Copy link
Copy Markdown
Author

Ty all for the suggestions. I've changed the category to site-editing and renamed the ability to core/navigation-menus-query to follow the query naming used by the user and content abilities. The category and ability names are still open to feedback :)

@Builder106
Builder106 requested a review from gziolo November 15, 2026 20:31
@jeffpaul

Copy link
Copy Markdown
Member

@Builder106 would you mind porting this over to https://github.com/WordPress/abilities?

@Builder106

Copy link
Copy Markdown
Author

@Builder106 would you mind porting this over to https://github.com/WordPress/abilities?

Moved here: WordPress/abilities#31

@jeffpaul

Copy link
Copy Markdown
Member

Closing in favor of WordPress/abilities#31.

@jeffpaul jeffpaul closed this Nov 22, 2026
@github-project-automation github-project-automation Bot moved this from Needs review to Done in WordPress AI Roadmap Nov 22, 2026
@jeffpaul jeffpaul removed this from the 1.6.0 milestone Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants