wp-rest-api

作者: wordpress

We need to translate the given text from English to Traditional Chinese. The text describes a skill related to WordPress REST API. We must preserve the name "wp-rest-api" if it appears, but it does not appear in the text. So we just translate the text. No extra commentary, no labels. Output only the translation. The text: "Register, validate, and debug WordPress REST API endpoints with schema enforcement and permission controls. Covers route registration via register_rest_route() and WP_REST_Controller subclasses, custom field exposure through register_rest_field and meta registration, and CPT/taxonomy REST exposure via show_in_rest Enforces schema validation, argument sanitization, and permission callbacks; supports cookie + nonce, application passwords, and custom auth plugins Includes triage workflow to..." We need to translate accurately. Note: "CPT" is Custom Post Type, "taxonomy" is taxonomy. "show_in_rest" is a parameter. "cookie + nonce" should be kept as is or translated? Probably keep as technical terms. "application

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 的 blueprint JSON 檔案時使用。當提及 blueprints、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 端點公開給客戶端。使用 @wordpress/abilities 套件在 JavaScript 中存取能力並進行權限檢查。需要 WordPress 6.9+...
official
wp-abilities-audit
wordpress
稽核 WordPress 外掛的 REST 介面,並產生標準化的稽核文件,提出 Abilities API 註冊建議。產生包含 YAML… 的 markdown 文件。
official
wp-abilities-verify
wordpress
Verify a WordPress plugin's Abilities API registrations: enumerate abilities, check that callback behavior matches each annotation's claim (the adversarial…
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 store/state/actions、區塊 viewScriptModule…)
official