wp_register_ability( string $name, array $args ): WP_Ability|null
- Since
- 6.9.0
- Source
wp-includes/abilities-api.php:290
Description
- Hook into the
wp_abilities_api_initaction. - Call
wp_register_ability()with a namespaced name and configuration. - Provide execute and permission callbacks.
Example:
function my_plugin_register_abilities(): void {
wp_register_ability(
'my-plugin/analyze-text',
array(
'label' => __( 'Analyze Text', 'my-plugin' ),
'description' => __( 'Performs sentiment analysis on provided text.', 'my-plugin' ),
'category' => 'text-processing',
'input_schema' => array(
'type' => 'string',
'description' => __( 'The text to be analyzed.', 'my-plugin' ),
'minLength' => 10,
'required' => true,
),
'output_schema' => array(
'type' => 'string',
'enum' => array( 'positive', 'negative', 'neutral' ),
'description' => __( 'The sentiment result: positive, negative, or neutral.', 'my-plugin' ),
'required' => true,
),
'execute_callback' => 'my_plugin_analyze_text',
'permission_callback' => 'my_plugin_can_analyze_text',
'meta' => array(
'annotations' => array(
'readonly' => true,
),
'public' => true,
),
)
);
}
add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' ); Naming Conventions
Ability names must follow these rules:
- Include a namespace prefix (e.g.,
my-plugin/my-ability). - Use only lowercase alphanumeric characters, dashes, and forward slashes.
- Use descriptive, action-oriented names (e.g.,
process-payment,generate-report).
Categories
Abilities must be organized into categories. Ability categories provide better discoverability and must be registered before the abilities that reference them:
function my_plugin_register_categories(): void {
wp_register_ability_category(
'text-processing',
array(
'label' => __( 'Text Processing', 'my-plugin' ),
'description' => __( 'Abilities for analyzing and transforming text.', 'my-plugin' ),
)
);
}
add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' ); Input and Output Schemas
Schemas define the expected structure, type, and constraints for ability inputs and outputs using JSON Schema syntax. They serve two critical purposes: automatic validation of data passed to and returned from abilities, and self-documenting API contracts for developers.
WordPress implements a validator based on a subset of the JSON Schema Version 4 specification (https://json-schema.org/specification-links.html#draft-4).
For details on supported JSON Schema properties and syntax, see the related WordPress REST API Schema documentation: https://developer.wordpress.org/rest-api/extending-the-rest-api/schema/#json-schema-basics
Defining schemas is mandatory when there is a value to pass or return.
They ensure data integrity, improve developer experience, and enable better documentation:
'input_schema' => array(
'type' => 'string',
'description' => __( 'The text to be analyzed.', 'my-plugin' ),
'minLength' => 10,
'required' => true,
),
'output_schema' => array(
'type' => 'string',
'enum' => array( 'positive', 'negative', 'neutral' ),
'description' => __( 'The sentiment result: positive, negative, or neutral.', 'my-plugin' ),
'required' => true,
), Callbacks
Execute Callback
The execute callback performs the ability's core functionality. It receives optional input data and returns either a result or WP_Error on failure.
function my_plugin_analyze_text( string $input ): string|WP_Error {
$score = My_Plugin::perform_sentiment_analysis( $input );
if ( is_wp_error( $score ) ) {
return $score;
}
return My_Plugin::interpret_sentiment_score( $score );
} Permission Callback
The permission callback determines whether the ability can be executed.
It receives the same input as the execute callback and must return a boolean or WP_Error. Common use cases include checking user capabilities, validating API keys, or verifying system state:
function my_plugin_can_analyze_text( string $input ): bool|WP_Error {
return current_user_can( 'edit_posts' );
} Client Exposure
Set the high-level public flag to make an ability available to clients such as the REST API, MCP, or AI agents:
'meta' => array(
'public' => true,
), The public flag seeds the default for each per-channel flag. For the REST API it seeds show_in_rest, which lets the ability be invoked via HTTP requests. Set a per-channel flag directly to override that default. For example, keep a public ability out of the REST API:
'meta' => array(
'public' => true,
'show_in_rest' => false,
),Compatibility
- WordPress
- since 6.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$namestring- The name of the ability. Must be a namespaced string containing a prefix, e.g.,
my-plugin/my-ability. Can only contain lowercase alphanumeric characters, dashes, and forward slashes. $argsarray
Return value
WP_Ability|null- The registered ability instance on success,
nullon failure.
Performance profile
How much work a call to wp_register_ability() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 12–25
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 37.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below wp_register_ability()
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_register_ability() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
doing_action() && $registry === null | 12 | doing_action(), ::WP_Abilities_Registry() |
doing_action() && $registry !== null | 17 | doing_action(), ::WP_Abilities_Registry(), ->register() |
!doing_action() | 25 | doing_action(), __(), esc_html(), sprintf(), _doing_it_wrong() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 37 instructions, 12–25 executed per call, 2 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- doing_action()Returns whether or not an action hook is currently being processed.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- WP_Abilities_Registry::get_instance()Utility method to retrieve the main instance of the registry class.
Used by · 1
- wp_register_core_abilities()Registers the default core abilities.
Source code
function wp_register_ability( string $name, array $args ): ?WP_Ability { if ( ! doing_action( 'wp_abilities_api_init' ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: 1: wp_abilities_api_init, 2: string value of the ability name. */ __( 'Abilities must be registered on the %1$s action. The ability %2$s was not registered.' ), '<code>wp_abilities_api_init</code>', '<code>' . esc_html( $name ) . '</code>' ), '6.9.0' ); return null; } $registry = WP_Abilities_Registry::get_instance(); if ( null === $registry ) { return null; } return $registry->register( $name, $args );}Changelog
Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/abilities-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.