Agent-skills wp-rest-api
Use when building, extending, or debugging WordPress REST API endpoints/routes: register_rest_route, WP_REST_Controller/controller classes, schema/argument validation, permission_callback/authentication, response shaping, register_rest_field/register_meta, or exposing CPTs/taxonomies via show_in_rest.
install
source · Clone the upstream repo
git clone https://github.com/WordPress/agent-skills
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/WordPress/agent-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/wp-rest-api" ~/.claude/skills/wordpress-agent-skills-wp-rest-api && rm -rf "$T"
manifest:
skills/wp-rest-api/SKILL.mdsource content
WP REST API
When to use
Use this skill when you need to:
- create or update REST routes/endpoints
- debug 401/403/404 errors or permission/nonce issues
- add custom fields/meta to REST responses
- expose custom post types or taxonomies via REST
- implement schema + argument validation
- adjust response links/embedding/pagination
Inputs required
- Repo root + target plugin/theme/mu-plugin (path to entrypoint).
- Desired namespace + version (e.g.
) and routes.my-plugin/v1 - Authentication mode (cookie + nonce vs application passwords vs auth plugin).
- Target WordPress version constraints (if below 6.9, call out).
Procedure
0) Triage and locate REST usage
- Run triage:
node skills/wp-project-triage/scripts/detect_wp_project.mjs
- Search for existing REST usage:
register_rest_routeWP_REST_Controllerrest_api_init
,show_in_rest
,rest_baserest_controller_class
If this is a full site repo, pick the specific plugin/theme before changing code.
1) Choose the right approach
- Expose CPT/taxonomy in
:wp/v2- Use
+show_in_rest => true
if needed.rest_base - Optionally provide
.rest_controller_class - Read
.references/custom-content-types.md
- Use
- Custom endpoints:
- Use
onregister_rest_route()
.rest_api_init - Prefer a controller class (
subclass) for anything non-trivial.WP_REST_Controller - Read
andreferences/routes-and-endpoints.md
.references/schema.md
- Use
2) Register routes safely (namespaces, methods, permissions)
- Use a unique namespace
; avoidvendor/v1
unless core.wp/* - Always provide
(usepermission_callback
for public endpoints).__return_true - Use
constants.WP_REST_Server::READABLE/CREATABLE/EDITABLE/DELETABLE - Return data via
orrest_ensure_response()
.WP_REST_Response - Return errors via
with an explicitWP_Error
.status
Read
references/routes-and-endpoints.md.
3) Validate/sanitize request args
- Define
withargs
,type
,default
,required
,validate_callback
.sanitize_callback - Prefer JSON Schema validation with
thenrest_validate_value_from_schema
.rest_sanitize_value_from_schema - Never read
/$_GET
directly inside endpoints; use$_POST
.WP_REST_Request
Read
references/schema.md.
4) Responses, fields, and links
- Do not remove core fields from default endpoints; add fields instead.
- Use
for computed fields;register_rest_field
withregister_meta
for meta.show_in_rest - For
/object
meta, define schema inarray
.show_in_rest.schema - If you need unfiltered post content (e.g., ToC plugins injecting HTML), request
to access?context=edit
(auth required). Pair withcontent.raw
to keep responses small._fields=content.raw - Add related resource links via
.WP_REST_Response::add_link()
Read
references/responses-and-fields.md.
5) Authentication and authorization
- For wp-admin/JS: cookie auth +
(actionX-WP-Nonce
).wp_rest - For external clients: application passwords (basic auth) or an auth plugin.
- Use capability checks in
(authorization), not just “logged in”.permission_callback
Read
references/authentication.md.
6) Client-facing behavior (discovery, pagination, embeds)
- Ensure discovery works (
header orLink
).<link rel="https://api.w.org/"> - Support
,_fields
,_embed
,_method
, pagination headers._envelope - Remember
is capped at 100.per_page
Read
references/discovery-and-params.md.
Verification
index includes your namespace./wp-json/
on your route returns schema (when provided).OPTIONS- Endpoint returns expected data; permission failures return 401/403 as appropriate.
- CPT/taxonomy routes appear under
whenwp/v2
is true.show_in_rest - Run repo lint/tests and any PHP/JS build steps.
Failure modes / debugging
- 404:
not firing, route typo, or permalinks off (userest_api_init
).?rest_route= - 401/403: missing nonce/auth, or
too strict.permission_callback
for missing_doing_it_wrong
: add it (usepermission_callback
if public).__return_true- Invalid params: missing/incorrect
schema or validation callbacks.args - Fields missing:
false, meta not registered, or CPT lacksshow_in_rest
support.custom-fields
Escalation
If version support or behavior is unclear, consult the REST API Handbook and core docs before inventing patterns.