wp-rest-api
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWP 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
当你需要以下操作时,可使用本技能:
- 创建或更新REST路由/端点
- 调试401/403/404错误或权限/nonce问题
- 为REST响应添加自定义字段/元数据
- 通过REST暴露自定义文章类型或分类法
- 实现Schema + 参数验证
- 调整响应链接/嵌入/分页
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).
- 仓库根目录 + 目标插件/主题/mu-plugin(入口文件路径)。
- 所需的命名空间 + 版本(例如 )以及路由。
my-plugin/v1 - 身份验证模式(Cookie + Nonce vs 应用密码 vs 身份验证插件)。
- 目标WordPress版本限制(如果低于6.9,请特别说明)。
Procedure
操作步骤
0) Triage and locate REST usage
0) 问题排查与REST用法定位
- 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.
- 运行排查脚本:
node skills/wp-project-triage/scripts/detect_wp_project.mjs
- 搜索现有的REST用法:
register_rest_routeWP_REST_Controllerrest_api_init- ,
show_in_rest,rest_baserest_controller_class
如果是完整站点仓库,请在修改代码前选择特定的插件/主题。
1) Choose the right approach
1) 选择合适的实现方式
- 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
- 在中暴露自定义文章类型/分类法:
wp/v2- 若需要,使用+
show_in_rest => true。rest_base - 可选择性提供。
rest_controller_class - 阅读。
references/custom-content-types.md
- 若需要,使用
- 自定义端点:
- 在钩子上使用
rest_api_init。register_rest_route() - 对于非简单功能,优先使用控制器类(的子类)。
WP_REST_Controller - 阅读和
references/routes-and-endpoints.md。references/schema.md
- 在
2) Register routes safely (namespaces, methods, permissions)
2) 安全注册路由(命名空间、方法、权限)
- 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.md- 使用唯一的命名空间;除非是核心功能,否则避免使用
vendor/v1。wp/* - 务必提供(公开端点可使用
permission_callback)。__return_true - 使用常量。
WP_REST_Server::READABLE/CREATABLE/EDITABLE/DELETABLE - 通过或
rest_ensure_response()返回数据。WP_REST_Response - 通过带有明确的
status返回错误信息。WP_Error
阅读。
references/routes-and-endpoints.md3) Validate/sanitize request args
3) 验证与清理请求参数
- 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.md- 定义包含、
type、default、required、validate_callback的sanitize_callback。args - 优先使用JSON Schema验证,结合和
rest_validate_value_from_schema。rest_sanitize_value_from_schema - 切勿在端点内直接读取/
$_GET;请使用$_POST。WP_REST_Request
阅读。
references/schema.md4) Responses, fields, and links
4) 响应、字段与链接
- 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.md- 不要从默认端点中移除核心字段;应添加字段而非删除。
- 对于计算字段,使用;对于元数据,使用带有
register_rest_field的show_in_rest。register_meta - 对于/
object类型的元数据,在array中定义Schema。show_in_rest.schema - 如果需要未过滤的文章内容(例如,目录插件注入HTML),请求时携带以访问
?context=edit(需要身份验证)。可搭配content.raw来缩小响应体积。_fields=content.raw - 通过添加相关资源链接。
WP_REST_Response::add_link()
阅读。
references/responses-and-fields.md5) Authentication and authorization
5) 身份验证与授权
- 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.md- 对于wp-admin/JS:Cookie认证 + (动作
X-WP-Nonce)。wp_rest - 对于外部客户端:应用密码(基础认证)或身份验证插件。
- 在中使用权限检查(授权),而不只是检查“是否登录”。
permission_callback
阅读。
references/authentication.md6) Client-facing behavior (discovery, pagination, embeds)
6) 客户端行为(发现、分页、嵌入)
- 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.md- 确保发现功能正常工作(头或
Link)。<link rel="https://api.w.org/"> - 支持、
_fields、_embed、_method、分页头。_envelope - 注意的上限为100。
per_page
阅读。
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.
- 索引中包含你的命名空间。
/wp-json/ - 对路由发送请求时返回Schema(如果已提供)。
OPTIONS - 端点返回预期数据;权限验证失败时返回正确的401/403状态码。
- 当设为true时,自定义文章类型/分类法路由出现在
show_in_rest下。wp/v2 - 运行仓库的代码检查/测试,以及任何PHP/JS构建步骤。
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
- 404错误:未触发、路由拼写错误,或固定链接未开启(可使用
rest_api_init)。?rest_route= - 401/403错误:缺少nonce/身份验证,或过于严格。
permission_callback - 因缺少触发
permission_callback:添加该回调(公开端点可使用_doing_it_wrong)。__return_true - 参数无效:缺少/错误的Schema或验证回调。
args - 字段缺失:设为false、元数据未注册,或自定义文章类型不支持
show_in_rest。custom-fields
Escalation
升级处理
If version support or behavior is unclear, consult the REST API Handbook and core docs before inventing patterns.
如果版本支持或行为不明确,请先查阅REST API手册和核心文档,不要自行创造实现模式。