|
| 1 | +# WordPress Abilities API |
| 2 | + |
| 3 | +## Why This Boilerplate Includes Abilities Support |
| 4 | + |
| 5 | +The project provides interfaces and Loader integration for the WordPress Abilities API to give you a head start when exposing your plugin's functionality. |
| 6 | + |
| 7 | +**What this integration offers:** |
| 8 | + |
| 9 | +- **Consistent structure** — The `Ability_Interface` enforces input/output schemas, permission checks, and annotations so every ability follows the same pattern. |
| 10 | +- **Automatic registration** — Categories are collected and registered automatically when you add an ability to the Loader. |
| 11 | +- **Type safety** — PHPStan and IDE autocompletion guide your implementation. |
| 12 | +- **No overhead when unused** — If you don't register abilities, no code runs. On WordPress < 6.9, the feature is silently skipped. |
| 13 | + |
| 14 | +To understand *what* the Abilities API is or *why* you might use it see the [official documentation](https://developer.wordpress.org/news/2025/11/introducing-the-wordpress-abilities-api/). |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +### 1. Create Category Class (Optional) |
| 22 | + |
| 23 | +Create `src/Abilities/Categories/Data_Retrieval.php`: |
| 24 | + |
| 25 | +```php |
| 26 | +<?php |
| 27 | + |
| 28 | +namespace Demo_Plugin\Abilities\Categories; |
| 29 | + |
| 30 | +use Demo_Plugin\Abilities\Ability_Category_Interface; |
| 31 | + |
| 32 | +/** |
| 33 | + * Category for abilities that retrieve data without modifications. |
| 34 | + */ |
| 35 | +class Data_Retrieval implements Ability_Category_Interface { |
| 36 | + |
| 37 | + public static function get_slug(): string { |
| 38 | + return 'data-retrieval'; |
| 39 | + } |
| 40 | + |
| 41 | + public static function get_label(): string { |
| 42 | + return __( 'Data Retrieval', 'demo-plugin' ); |
| 43 | + } |
| 44 | + |
| 45 | + public static function get_description(): string { |
| 46 | + return __( 'Abilities that retrieve and return data from the WordPress site.', 'demo-plugin' ); |
| 47 | + } |
| 48 | + |
| 49 | + public static function get_meta(): array { |
| 50 | + return array(); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### 2. Create Ability Class |
| 56 | + |
| 57 | +Create `src/Abilities/My_Ability.php`: |
| 58 | + |
| 59 | +```php |
| 60 | +<?php |
| 61 | + |
| 62 | +namespace Demo_Plugin\Abilities; |
| 63 | + |
| 64 | +use Demo_Plugin\Abilities\Categories\Data_Retrieval; |
| 65 | +use WP_Error; |
| 66 | + |
| 67 | +class My_Ability implements Ability_Interface { |
| 68 | + |
| 69 | + public static function get_name(): string { |
| 70 | + return 'demo-plugin/my-ability'; |
| 71 | + } |
| 72 | + |
| 73 | + public static function get_label(): string { |
| 74 | + return __( 'My Ability', 'demo-plugin' ); |
| 75 | + } |
| 76 | + |
| 77 | + public static function get_description(): string { |
| 78 | + return __( 'Does something useful.', 'demo-plugin' ); |
| 79 | + } |
| 80 | + |
| 81 | + public static function get_category(): string { |
| 82 | + return Data_Retrieval::class; |
| 83 | + } |
| 84 | + |
| 85 | + public static function get_input_schema(): array { |
| 86 | + return array( |
| 87 | + 'type' => 'object', |
| 88 | + 'properties' => array( |
| 89 | + 'post_id' => array( 'type' => 'integer', 'minimum' => 1 ), |
| 90 | + ), |
| 91 | + 'required' => array( 'post_id' ), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + public static function get_output_schema(): array { |
| 96 | + return array( |
| 97 | + 'type' => 'object', |
| 98 | + 'properties' => array( |
| 99 | + 'success' => array( 'type' => 'boolean' ), |
| 100 | + 'title' => array( 'type' => 'string' ), |
| 101 | + ), |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + public static function get_annotations(): array { |
| 106 | + return array( |
| 107 | + 'readonly' => true, |
| 108 | + 'destructive' => false, |
| 109 | + 'idempotent' => true, |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + public static function show_rest(): bool { |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + public static function check_permissions( mixed $input = null ): bool|WP_Error { |
| 118 | + return current_user_can( 'read' ); |
| 119 | + } |
| 120 | + |
| 121 | + public static function execute( mixed $input = null ): mixed { |
| 122 | + $post = get_post( $input['post_id'] ); |
| 123 | + |
| 124 | + if ( ! $post ) { |
| 125 | + return new WP_Error( 'not_found', 'Post not found.', array( 'status' => 404 ) ); |
| 126 | + } |
| 127 | + |
| 128 | + return array( 'success' => true, 'title' => $post->post_title ); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### 3. Register Ability |
| 134 | + |
| 135 | +In `Demo_Plugin.php`: |
| 136 | + |
| 137 | +```php |
| 138 | +$this->loader->add_ability( Abilities\My_Ability::class ); |
| 139 | +``` |
| 140 | + |
| 141 | +Categories are automatically registered via the Loader when abilities reference them. |
| 142 | + |
| 143 | +## Ability Interface Reference |
| 144 | + |
| 145 | +| Method | Returns | Purpose | |
| 146 | +|--------|---------|---------| |
| 147 | +| `get_name()` | `string` | Unique ID: `namespace/ability-name` | |
| 148 | +| `get_label()` | `string` | Display name | |
| 149 | +| `get_description()` | `string` | What the ability does | |
| 150 | +| `get_category()` | `string` | Category class name | |
| 151 | +| `get_input_schema()` | `array` | JSON Schema for input | |
| 152 | +| `get_output_schema()` | `array` | JSON Schema for output | |
| 153 | +| `get_annotations()` | `array` | Behavioral hints (readonly, destructive, idempotent) | |
| 154 | +| `show_rest()` | `bool` | Expose via REST API | |
| 155 | +| `check_permissions($input)` | `bool\|WP_Error` | Permission check | |
| 156 | +| `execute($input)` | `mixed` | Main logic | |
| 157 | + |
| 158 | +## Category Interface Reference |
| 159 | + |
| 160 | +| Method | Returns | Purpose | |
| 161 | +|--------|---------|---------| |
| 162 | +| `get_slug()` | `string` | Unique slug (lowercase, hyphens only) | |
| 163 | +| `get_label()` | `string` | Display name | |
| 164 | +| `get_description()` | `string` | Category purpose | |
| 165 | +| `get_meta()` | `array` | Optional metadata | |
| 166 | + |
| 167 | +## Usage |
| 168 | + |
| 169 | +```php |
| 170 | +$ability = wp_get_ability( 'demo-plugin/my-ability' ); |
| 171 | +$result = $ability->execute( array( 'post_id' => 123 ) ); |
| 172 | + |
| 173 | +if ( is_wp_error( $result ) ) { |
| 174 | + echo $result->get_error_message(); |
| 175 | +} |
| 176 | +``` |
0 commit comments