wp-rest-api

作者: wordpress

注册、验证和调试WordPress REST API端点,包含模式强制与权限控制。涵盖通过register_rest_route()和WP_REST_Controller子类进行路由注册,通过register_rest_field和元数据注册暴露自定义字段,以及通过show_in_rest暴露自定义文章类型/分类法REST接口。强制实施模式验证、参数清理和权限回调;支持Cookie+Nonce、应用密码及自定义认证插件。包含故障排查工作流以...

npx skills add https://github.com/wordpress/agent-skills --skill wp-rest-api

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 7.0, 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.

来自 wordpress 的更多技能

blueprint
wordpress
在创建、编辑或审查WordPress Playground蓝图JSON文件时使用。当提及蓝图、Playground配置或请求时触发…
official
wordpress-router
wordpress
对WordPress代码库进行分类,并根据插件、主题、区块及核心检出结果路由至正确的工作流程。运行自动化项目分类以识别仓库类型(插件、主题、区块主题、Gutenberg区块、WP核心)及可用工具。输出分类结果及基于用户意图和项目类型的决策树路由至领域特定技能。需要仓库根目录访问权限及bash/Node文件系统操作;部分工作流程需WP-CLI。目标环境为WordPress 6.9+及PHP 7.2.24+;...
official
wp-abilities-api
wordpress
WordPress Abilities API 注册、REST 暴露及客户端消费,适用于 WordPress 6.9+。使用 wp_register_ability() 和 wp_register_ability_category() 在 PHP 中注册能力和类别,包含稳定 ID、标签和元数据。通过设置 meta.show_in_rest: true,将能力暴露给客户端,使用 /wp-json/wp-abilities/v1/ REST 端点。在 JavaScript 中使用 @wordpress/abilities 包消费能力,实现客户端访问和权限检查。需要 WordPress 6.9+...
official
wp-abilities-audit
wordpress
审计WordPress插件的REST接口面,并生成一份标准化的审计文档,提出Abilities API注册建议。生成一份包含YAML……的markdown文档。
official
wp-abilities-verify
wordpress
验证WordPress插件的Abilities API注册:枚举能力,检查回调行为是否与每个注解的声明相符(对抗性…
official
wp-block-development
wordpress
WordPress区块开发(针对Gutenberg):元数据、注册、渲染及构建工作流。涵盖区块创建、block.json配置、静态与动态渲染,以及使用register_block_type_from_metadata()进行服务端PHP注册。强制使用apiVersion: 3以确保与WordPress 6.9+兼容,包括iframe编辑器支持和样式隔离。处理属性序列化、弃用/迁移以避免"无效区块"错误,以及内部区块组合。包括...
official
wp-block-themes
wordpress
WordPress区块主题开发:theme.json、模板、样式块及站点编辑器故障排查。涵盖theme.json编辑(预设、设置、逐块样式)、模板与模板部件、样式块及WordPress 6.9+版本中的样式变体。包含用于检测主题根目录和区块主题结构的分类脚本,以及创建新主题或转换经典主题的引导流程。提供样式层级问题、用户自定义覆盖及站点编辑器调试的工作流程。
official
wp-interactivity-api
wordpress
在构建或调试WordPress Interactivity API功能时使用(data-wp-*指令、@wordpress/interactivity存储/状态/动作、块viewScriptModule…)
official