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.
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.
Stronger alternatives
This repository is archived — consider an actively maintained alternative.
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 showinrest => true + restbase if needed. - Optionally provide restcontroller_class. - Read references/custom-content-types.md.
Custom endpoints:
- Use registerrestroute() on restapiinit. - Prefer a controller class (WPRESTController subclass) for anything non-trivial. - Read references/routes-and-endpoints.md and references/schema.md.
Use a unique namespace vendor/v1; avoid wp/* unless core.
Always provide permission_callback (use __return_true for public endpoints).
Use WPRESTServer::READABLE/CREATABLE/EDITABLE/DELETABLE constants.
Return data via restensureresponse() or WPRESTResponse.
Return errors via WP_Error with an explicit status.
Read references/routes-and-endpoints.md.
3) Validate/sanitize request args
Define args with type, default, required, validatecallback, sanitizecallback.
Prefer JSON Schema validation with restvalidatevaluefromschema then restsanitizevaluefromschema.
Never read $GET/$POST directly inside endpoints; use WPRESTRequest.
Read references/schema.md.
4) Responses, fields, and links
Do not remove core fields from default endpoints; add fields instead.
Use registerrestfield for computed fields; registermeta with showin_rest for meta.
For object/array meta, define schema in showinrest.schema.
If you need unfiltered post content (e.g., ToC plugins injecting HTML), request ?context=edit to access content.raw (auth required). Pair with _fields=content.raw to keep responses small.
Add related resource links via WPRESTResponse::add_link().
Read references/responses-and-fields.md.
5) Authentication and authorization
For wp-admin/JS: cookie auth + X-WP-Nonce (action wp_rest).
For external clients: application passwords (basic auth) or an auth plugin.
Use capability checks in permission_callback (authorization), not just “logged in”.