wp-rest-api
Original:🇺🇸 English
Not Translated
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.
5installs
Sourceautomattic/agent-skills
Added on
NPX Install
npx skill4agent add automattic/agent-skills wp-rest-apiSKILL.md 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 => trueif needed.rest_base - Optionally provide .
rest_controller_class - Read .
references/custom-content-types.md
- Use
- Custom endpoints:
- Use on
register_rest_route().rest_api_init - Prefer a controller class (subclass) for anything non-trivial.
WP_REST_Controller - Read and
references/routes-and-endpoints.md.references/schema.md
- Use
2) Register routes safely (namespaces, methods, permissions)
- Use a unique namespace ; avoid
vendor/v1unless core.wp/* - Always provide (use
permission_callbackfor public endpoints).__return_true - Use constants.
WP_REST_Server::READABLE/CREATABLE/EDITABLE/DELETABLE - Return data via or
rest_ensure_response().WP_REST_Response - Return errors via with an explicit
WP_Error.status
Read .
references/routes-and-endpoints.md3) Validate/sanitize request args
- Define with
args,type,default,required,validate_callback.sanitize_callback - Prefer JSON Schema validation with then
rest_validate_value_from_schema.rest_sanitize_value_from_schema - Never read /
$_GETdirectly inside endpoints; use$_POST.WP_REST_Request
Read .
references/schema.md4) Responses, fields, and links
- Do not remove core fields from default endpoints; add fields instead.
- Use for computed fields;
register_rest_fieldwithregister_metafor meta.show_in_rest - For /
objectmeta, 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.rawto keep responses small._fields=content.raw - Add related resource links via .
WP_REST_Response::add_link()
Read .
references/responses-and-fields.md5) Authentication and authorization
- For wp-admin/JS: cookie auth + (action
X-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.md6) Client-facing behavior (discovery, pagination, embeds)
- Ensure discovery works (header or
Link).<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.mdVerification
- 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 when
wp/v2is 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 (use
rest_api_init).?rest_route= - 401/403: missing nonce/auth, or too strict.
permission_callback - for missing
_doing_it_wrong: add it (usepermission_callbackif public).__return_true - Invalid params: missing/incorrect schema or validation callbacks.
args - Fields missing: false, meta not registered, or CPT lacks
show_in_restsupport.custom-fields
Escalation
If version support or behavior is unclear, consult the REST API Handbook and core docs before inventing patterns.