一键导入
wp-rest-api
WordPress REST endpoint development and debugging. Always-active rules when working with REST routes, API authentication, or data exposure via JSON.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
WordPress REST endpoint development and debugging. Always-active rules when working with REST routes, API authentication, or data exposure via JSON.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Audit and improve web accessibility following WCAG 2.1 guidelines. Use when asked to "improve accessibility", "a11y audit", "WCAG compliance", "screen reader support", "keyboard navigation", or "make accessible".
Optimize web performance for faster loading and better user experience. Use when asked to "speed up my site", "optimize performance", "reduce load time", "fix slow loading", "improve page speed", or "performance audit".
Optimize for search engine visibility and ranking. Use when asked to "improve SEO", "optimize for search", "fix meta tags", "add structured data", "sitemap optimization", or "search engine optimization".
WordPress operations with WP-CLI — search-replace, plugin/theme management, cron, cache, DB export/import, automation. Always-active rules when using wp commands.
WCAG 2.2 checklist for accessible markup. Always-active rules for every generated interface.
How to use Figma MCP and Variables2CSS to read designs from Figma and generate code consistent with tokens. Use when the designer shares a Figma link.
| name | wp-rest-api |
| description | WordPress REST endpoint development and debugging. Always-active rules when working with REST routes, API authentication, or data exposure via JSON. |
| user-invocable | false |
Skill for creating, managing, and debugging REST endpoints in WordPress 6.x+. Covers route registration, authentication, input validation, security, and integration with CPTs/taxonomies.
Apply this skill when the task involves:
Before starting, verify:
{{TEXT_DOMAIN}}/v1)Before creating new endpoints, search the codebase:
register_rest_route → custom routes already registered
WP_REST_Controller → extended controllers
rest_api_init → registration hooks
show_in_rest → exposed CPTs/taxonomies
register_rest_field → custom fields added
Check for namespace conflicts and registration patterns already in use.
| Scenario | Approach |
|---|---|
| Expose a CPT or taxonomy | 'show_in_rest' => true in the CPT registration |
| Add fields to existing endpoints | register_rest_field() or register_meta() with show_in_rest |
| Custom logic (calculations, aggregations, actions) | register_rest_route() with a dedicated handler |
| Full CRUD endpoint | Extend WP_REST_Controller |
Mandatory rules:
add_action('rest_api_init', function () {
register_rest_route('{{TEXT_DOMAIN}}/v1', '/items', [
'methods' => WP_REST_Server::READABLE, // Use constants, not strings
'callback' => 'handle_get_items',
'permission_callback' => 'check_items_permission', // MANDATORY — never '__return_true' in production if data is sensitive
'args' => get_items_args_schema(),
]);
});
{{TEXT_DOMAIN}}/v1 — never register under wp/v2permission_callback always present: WordPress 5.5+ logs a _doing_it_wrong if missingWP_REST_Server::READABLE, ::CREATABLE, ::EDITABLE, ::DELETABLErest_ensure_response() or new WP_REST_Response($data, $status)Define the schema for every argument — never access $_GET/$_POST directly:
function get_items_args_schema(): array {
return [
'per_page' => [
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
],
'search' => [
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
],
'status' => [
'type' => 'string',
'enum' => ['aperto', 'chiuso', 'in_arrivo'],
'default' => 'aperto',
],
];
}
type: string, integer, boolean, array, objectenum for allowed values, minimum/maximum for rangessanitize_callback cleans the data, validate_callback rejects it if invalidFor ACF/post meta metadata — expose via register_meta():
register_meta('post', 'importo_bando', [
'object_subtype' => 'bando',
'type' => 'number',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function () {
return current_user_can('edit_posts');
},
]);
For computed values — use register_rest_field():
register_rest_field('bando', 'stato_calcolato', [
'get_callback' => function ($object) {
return calcola_stato_bando($object['id']);
},
'schema' => [
'type' => 'string',
'description' => 'Automatically calculated bando status',
'context' => ['view', 'edit'],
],
]);
context to control where the field appears (view, edit, embed)| Context | Method | Notes |
|---|---|---|
| JavaScript in wp-admin | Cookie + X-WP-Nonce | wp_create_nonce('wp_rest') — automatic with wp.apiFetch |
| External apps / CI | Application Passwords | Dedicated WP user with minimal capabilities |
| Authentication plugins | JWT / OAuth | Use established plugins, do not reinvent |
permission_callback — always check capabilities, not roles:
function check_items_permission(WP_REST_Request $request): bool|WP_Error {
if (!current_user_can('edit_posts')) {
return new WP_Error(
'rest_forbidden',
__('Permesso negato.', '{{TEXT_DOMAIN}}'),
['status' => 403]
);
}
return true;
}
'permission_callback' => '__return_true'current_user_can()/wp-json/{{TEXT_DOMAIN}}/v1?_fields=id,title,stato to reduce payload?_embed to include related resources inlineX-WP-Total, X-WP-TotalPages headers; maximum 100 per pageCache-Control headers for high-traffic public endpoints/wp-json/) shows your namespaceOPTIONS on routes returns the schema401 on protected endpoints403400 with a clear messageshow_in_rest appear under wp/v2| Problem | Likely cause | Solution |
|---|---|---|
| 404 on the endpoint | rest_api_init hook not executed, wrong route name, permalinks not enabled | Verify the code is loaded; enable pretty permalinks; check the namespace |
| 401 / Cookie nonce mismatch | Nonce missing or expired in the JS request | Use wp.apiFetch which handles the nonce automatically, or pass X-WP-Nonce in the header |
| 403 Forbidden | permission_callback rejects; user without capability | Verify the required capability and user role |
| Custom field missing | show_in_rest not set, register_meta without object_subtype | Add show_in_rest => true and specify the subtype |
| Schema not validated | validate_callback not defined or not used | Use rest_validate_request_arg as callback |
| Corrupted serialized data | register_meta of type array/object without show_in_rest.schema | Define the full schema in show_in_rest['schema'] |
wp/v2 namespace — it is reserved for core$_GET, $_POST, $_REQUEST — use $request->get_param() or $request->get_json_params()echo/die() — always return a WP_REST_Response or WP_Error objectpermission_callback — even if the endpoint is public, use '__return_true'$wpdb->prepare() if you need direct queries