Skip to content

Commit 55dc875

Browse files
Post publish tweaks
1 parent 9159ede commit 55dc875

File tree

1 file changed

+19
-40
lines changed

1 file changed

+19
-40
lines changed

dev-notes/abilities-api.md

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The Abilities API automatically exposes registered abilities through REST API en
4848
- `GET /wp-abilities/v1/categories/{slug}` – Get a single ability category
4949
- `GET /wp-abilities/v1/abilities` – List all abilities
5050
- `GET /wp-abilities/v1/abilities/{name}` – Get a single ability
51-
- `GET|POST /wp-abilities/v1/abilities/{name}/run` – Execute an ability
51+
- `GET|POST|DELETE /wp-abilities/v1/abilities/{name}/run` – Execute an ability
5252

5353
#### 3\. Hooks
5454

@@ -102,23 +102,13 @@ function my_plugin_register_abilities() {
102102
'description' => __( 'Retrieves the total number of published posts.', 'my-plugin' ),
103103
'category' => 'content-management',
104104
'input_schema' => array(
105-
'type' => 'object',
106-
'properties' => array(
107-
'post_type' => array(
108-
'type' => 'string',
109-
'description' => __( 'The post type to count.', 'my-plugin' ),
110-
'default' => 'post',
111-
),
112-
),
105+
'type' => 'string',
106+
'description' => __( 'The post type to count.', 'my-plugin' ),
107+
'default' => 'post',
113108
),
114109
'output_schema' => array(
115-
'type' => 'object',
116-
'properties' => array(
117-
'count' => array(
118-
'type' => 'integer',
119-
'description' => __( 'The number of published posts.', 'my-plugin' ),
120-
),
121-
),
110+
'type' => 'integer',
111+
'description' => __( 'The number of published posts.', 'my-plugin' ),
122112
),
123113
'execute_callback' => 'my_plugin_get_post_count',
124114
'permission_callback' => function() {
@@ -132,19 +122,17 @@ function my_plugin_register_abilities() {
132122
* Execute callback for get-post-count ability.
133123
*/
134124
function my_plugin_get_post_count( $input ) {
135-
$post_type = $input['post_type'] ?? 'post';
125+
$post_type = $input ?? 'post';
136126

137127
$count = wp_count_posts( $post_type );
138128

139-
return array(
140-
'count' => (int) $count->publish,
141-
);
129+
return (int) $count->publish;
142130
}
143131
```
144132

145133
#### More Complex Example
146134

147-
Here's an example with more advanced input validation and error handling:
135+
Here's an example with more advanced input and output schemas, input validation, and error handling:
148136

149137
```php
150138
<?php
@@ -254,7 +242,7 @@ Abilities must be assigned to a category. Categories provide better discoverabil
254242

255243
### JSON Schema Validation
256244

257-
The Abilities API uses JSON Schema for input and output validation. WordPress implements a validator based on a subset of JSON Schema Version 4\. The schemas serve two purposes:
245+
The Abilities API uses [JSON Schema](https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/) for input and output validation. WordPress implements a validator based on a subset of JSON Schema Version 4\. The schemas serve two purposes:
258246

259247
1. Automatic validation of data passed to and returned from abilities
260248
2. Self-documenting API contracts for developers
@@ -263,7 +251,7 @@ Defining schemas is mandatory when there is a value to pass or return.
263251

264252
### Using REST API Endpoints
265253

266-
Developers can also enable Abilities to support the default REST API endpoints. This is possible by setting the `meta.show_in_rest` argument to `true` when registering an ability.
254+
Developers can also enable Abilities to support the default [REST API](https://developer.wordpress.org/rest-api/) endpoints. This is possible by setting the `meta.show_in_rest` argument to `true` when registering an ability.
267255

268256
```php
269257
wp_register_ability(
@@ -273,23 +261,13 @@ Developers can also enable Abilities to support the default REST API endpoints.
273261
'description' => __( 'Retrieves the total number of published posts.', 'my-plugin' ),
274262
'category' => 'content-management',
275263
'input_schema' => array(
276-
'type' => 'object',
277-
'properties' => array(
278-
'post_type' => array(
279-
'type' => 'string',
280-
'description' => __( 'The post type to count.', 'my-plugin' ),
281-
'default' => 'post',
282-
),
283-
),
264+
'type' => 'string',
265+
'description' => __( 'The post type to count.', 'my-plugin' ),
266+
'default' => 'post',
284267
),
285268
'output_schema' => array(
286-
'type' => 'object',
287-
'properties' => array(
288-
'count' => array(
289-
'type' => 'integer',
290-
'description' => __( 'The number of published posts.', 'my-plugin' ),
291-
),
292-
),
269+
'type' => 'integer',
270+
'description' => __( 'The number of published posts.', 'my-plugin' ),
293271
),
294272
'execute_callback' => 'my_plugin_get_post_count',
295273
'permission_callback' => function() {
@@ -408,8 +386,7 @@ if ( function_exists( 'wp_register_ability' ) ) {
408386

409387
```php
410388
if ( class_exists( 'WP_Ability' ) ) {
411-
add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
412-
}
389+
add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' ); }
413390
```
414391

415392
### Further Resources
@@ -421,3 +398,5 @@ if ( class_exists( 'WP_Ability' ) ) {
421398
- [Core Trac Ticket \#64098](https://core.trac.wordpress.org/ticket/64098)
422399
- [Abilities API Handbook](https://make.wordpress.org/ai/handbook/projects/abilities-api/)
423400
- [AI Building Blocks Initiative](https://make.wordpress.org/ai/2025/07/17/abilities-api/)
401+
402+
*Props to **@**gziolo for pre-publish review.*

0 commit comments

Comments
 (0)