PluginBench
Skill
Official
Review
Audit score 70

wp-rest-api

wordpress/agent-skills

Build, extend, and debug WordPress REST API endpoints with schema validation, authentication, and custom fields.

What is wp-rest-api?

This skill helps you create and modify WordPress REST API routes, handle authentication and permissions, validate request arguments, and expose custom post types and taxonomies via REST. Use it when building custom endpoints, debugging API errors, or adding fields to REST responses.

  • Register custom REST routes and endpoints with proper namespacing and HTTP methods
  • Implement schema validation and argument sanitization for request parameters
  • Set up authentication (cookie+nonce, application passwords) and permission callbacks
  • Add custom fields and meta to REST responses via register_rest_field and register_meta
  • Expose custom post types and taxonomies via show_in_rest configuration
  • Debug 401/403/404 errors and permission/nonce issues

How to install wp-rest-api

npx skills add https://github.com/wordpress/agent-skills --skill wp-rest-api
Prerequisites
  • WordPress 6.9+ (PHP 7.2.24+)
  • Access to plugin/theme/mu-plugin entrypoint files
  • WP-CLI for some workflows (optional)
  • Understanding of WordPress capabilities and permissions system
Claude Code
Cursor
Windsurf
Cline

How to use wp-rest-api

  1. 1.Run project triage to identify existing REST usage and locate your plugin/theme
  2. 2.Choose your approach: expose a CPT/taxonomy via show_in_rest or register custom endpoints
  3. 3.Register routes with unique namespace, HTTP methods, and permission_callback on rest_api_init hook
  4. 4.Define args with type, validation, and sanitization callbacks; use JSON Schema for complex validation
  5. 5.Add custom fields via register_rest_field or register_meta with show_in_rest configuration
  6. 6.Test endpoints at /wp-json/, verify OPTIONS returns schema, and confirm proper 401/403 responses

Use cases

Good for
  • Create a custom endpoint to expose plugin-specific data with authentication
  • Add computed fields to existing post types without modifying core endpoints
  • Expose a custom post type via the REST API with proper schema validation
  • Debug why a REST endpoint returns 403 Forbidden or 404 Not Found
  • Configure application password authentication for external API clients
Who it's for
  • WordPress plugin developers building custom APIs
  • Theme developers extending REST functionality
  • Full-stack developers integrating WordPress with external applications
  • WordPress site maintainers debugging API authentication issues

wp-rest-api FAQ

How do I expose a custom post type via REST?

Add show_in_rest => true to your register_post_type() call. Optionally set rest_base for a custom route and rest_controller_class to use a custom controller. See references/custom-content-types.md for details.

What's the difference between register_rest_field and register_meta?

register_rest_field adds computed fields calculated on-the-fly; register_meta exposes post/term/user meta. For meta, set show_in_rest => true in register_meta() and define schema for complex types.

How do I handle authentication for external API clients?

Use application passwords (basic auth) or an auth plugin. For wp-admin/JS, use cookie auth with X-WP-Nonce header (action wp_rest). Always implement capability checks in permission_callback.

Why am I getting a 404 on my custom endpoint?

Check that rest_api_init hook is firing, route path is spelled correctly, permalinks are enabled (or use ?rest_route= parameter), and namespace is registered. Verify with /wp-json/ index.

How do I add custom fields to REST responses without modifying core endpoints?

Use register_rest_field() to add computed fields or register_meta() to expose post meta. Never remove core fields; only add new ones. Define schema for complex data types.

Full instructions (SKILL.md)

Source of truth, from wordpress/agent-skills.


name: wp-rest-api description: "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." compatibility: "Targets WordPress 6.9+ (PHP 7.2.24+). Filesystem-based agent with bash + node. Some workflows require WP-CLI."

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. my-plugin/v1) and routes.
  • 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

  1. Run triage:
    • node skills/wp-project-triage/scripts/detect_wp_project.mjs
  2. Search for existing REST usage:
    • register_rest_route
    • WP_REST_Controller
    • rest_api_init
    • show_in_rest, rest_base, rest_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 + rest_base if needed.
    • Optionally provide rest_controller_class.
    • Read references/custom-content-types.md.
  • Custom endpoints:
    • Use register_rest_route() on rest_api_init.
    • Prefer a controller class (WP_REST_Controller subclass) for anything non-trivial.
    • Read references/routes-and-endpoints.md and references/schema.md.

2) Register routes safely (namespaces, methods, permissions)

  • Use a unique namespace vendor/v1; avoid wp/* unless core.
  • Always provide permission_callback (use __return_true for public endpoints).
  • Use WP_REST_Server::READABLE/CREATABLE/EDITABLE/DELETABLE constants.
  • Return data via rest_ensure_response() or WP_REST_Response.
  • 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, validate_callback, sanitize_callback.
  • Prefer JSON Schema validation with rest_validate_value_from_schema then rest_sanitize_value_from_schema.
  • Never read $_GET/$_POST directly inside endpoints; use WP_REST_Request.

Read references/schema.md.

4) Responses, fields, and links

  • Do not remove core fields from default endpoints; add fields instead.
  • Use register_rest_field for computed fields; register_meta with show_in_rest for meta.
  • For object/array meta, define schema in show_in_rest.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 WP_REST_Response::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”.

Read references/authentication.md.

6) Client-facing behavior (discovery, pagination, embeds)

  • Ensure discovery works (Link header or <link rel="https://api.w.org/">).
  • Support _fields, _embed, _method, _envelope, pagination headers.
  • Remember per_page is capped at 100.

Read references/discovery-and-params.md.

Verification

  • /wp-json/ index includes your namespace.
  • OPTIONS on your route returns schema (when provided).
  • Endpoint returns expected data; permission failures return 401/403 as appropriate.
  • CPT/taxonomy routes appear under wp/v2 when show_in_rest is true.
  • Run repo lint/tests and any PHP/JS build steps.

Failure modes / debugging

  • 404: rest_api_init not firing, route typo, or permalinks off (use ?rest_route=).
  • 401/403: missing nonce/auth, or permission_callback too strict.
  • _doing_it_wrong for missing permission_callback: add it (use __return_true if public).
  • Invalid params: missing/incorrect args schema or validation callbacks.
  • Fields missing: show_in_rest false, meta not registered, or CPT lacks custom-fields support.

Escalation

If version support or behavior is unclear, consult the REST API Handbook and core docs before inventing patterns.