wppaste
WordPress

WP_Abilities_Registry::register( string $name, array $args ): WP_Ability|null

Since
6.9.0
Source
wp-includes/abilities-api/class-wp-abilities-registry.php:85
Registers a new ability.

Description

Do not use this method directly. Instead, use the wp_register_ability() function.

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. The name must be a string containing a namespace prefix, i.e. my-plugin/my-ability. It can only contain lowercase alphanumeric characters, dashes and the forward slash.
$argsarray

Return value

WP_Ability|null
The registered ability instance on success, null on failure.

Performance profile

How much work a call to WP_Abilities_Registry::register() 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
13–48

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 113.

Plugin surface
1 hook

Third-party callbacks on 'wp_register_ability_args' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

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 · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Abilities_Registry::register() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$name13__(), _doing_it_wrong()
$name && ->is_registered()24->is_registered(), __(), esc_html(), sprintf(), _doing_it_wrong()
$name && !->is_registered() && !isset($args)34–35->is_registered(), apply_filters()
$name && !->is_registered() && !is_a()34->is_registered(), apply_filters(), is_a(), __(), _doing_it_wrong()
$name && !->is_registered() && wp_has_ability_category()40–41->is_registered(), apply_filters(), wp_has_ability_category()
$name && !->is_registered() && isset($args) && wp_has_ability_category() && !is_a()40->is_registered(), apply_filters(), wp_has_ability_category(), is_a(), __(), _doing_it_wrong()
$name && !->is_registered() && is_a()41–42->is_registered(), apply_filters(), is_a()
$name && !->is_registered() && isset($args) && !wp_has_ability_category()44->is_registered(), apply_filters(), wp_has_ability_category(), __(), esc_html(), esc_html(), sprintf(), _doing_it_wrong()
$name && !->is_registered() && isset($args) && wp_has_ability_category() && is_a()47–48->is_registered(), apply_filters(), wp_has_ability_category(), is_a()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11313–487
8.511313–487
8.411313–4873 fewer instructions than PHP 8.3
8.311616–517
8.211616–517
8.111616–517
7.411616–517

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.

Hooks and filters fired · 1

One hook fires while WP_Abilities_Registry::register() runs, in this order:

  1. apply_filters( wp_register_ability_args )filterline 139 (+54 into the body)

    Filters the ability arguments before they are validated and used to instantiate the ability.

Uses · 6

Source code

	public function register( string $name, array $args ): ?WP_Ability {		if ( ! preg_match( '/^[a-z0-9-]+\/[a-z0-9-]+$/', $name ) ) {			_doing_it_wrong(				__METHOD__,				__(					'Ability name must be a string containing a namespace prefix, i.e. "my-plugin/my-ability". It can only contain lowercase alphanumeric characters, dashes and the forward slash.'				),				'6.9.0'			);			return null;		} 		if ( $this->is_registered( $name ) ) {			_doing_it_wrong(				__METHOD__,				/* translators: %s: Ability name. */				sprintf( __( 'Ability "%s" is already registered.' ), esc_html( $name ) ),				'6.9.0'			);			return null;		} 		/**		 * Filters the ability arguments before they are validated and used to instantiate the ability.		 *		 * @since 6.9.0		 *		 * @param array<string, mixed> $args {		 *     An associative array of arguments for the ability.		 *		 *     @type string               $label                 The human-readable label for the ability.		 *     @type string               $description           A detailed description of what the ability does.		 *     @type string               $category              The ability category slug this ability belongs to.		 *     @type callable             $execute_callback      A callback function to execute when the ability is invoked.		 *                                                       Receives optional mixed input and returns mixed result or WP_Error.		 *     @type callable             $permission_callback   A callback function to check permissions before execution.		 *                                                       Receives optional mixed input and returns bool or WP_Error.		 *     @type array<string, mixed> $input_schema          Optional. JSON Schema definition for the ability's input.		 *     @type array<string, mixed> $output_schema         Optional. JSON Schema definition for the ability's output.		 *     @type array<string, mixed> $meta                  {		 *         Optional. Additional metadata for the ability.		 *		 *         @type array<string, bool|string> $annotations  Optional. Annotation metadata for the ability.		 *         @type bool                       $public       Optional. Whether the ability is meant to be		 *                                                        available to clients such as the REST API, MCP, or AI		 *                                                        agents. Seeds the default for per-channel flags like		 *                                                        `$show_in_rest`. Defaults to false.		 *         @type bool                       $show_in_rest Optional. Whether to expose this ability in the REST API.		 *                                                        Default is the value of `$public` when set, false otherwise.		 *     }		 *     @type string               $ability_class         Optional. Custom class to instantiate instead of WP_Ability.		 * }		 * @param string               $name The name of the ability, with its namespace.		 */		$args = apply_filters( 'wp_register_ability_args', $args, $name ); 		// Validate ability category exists if provided (will be validated as required in WP_Ability).		if ( isset( $args['category'] ) ) {			if ( ! wp_has_ability_category( $args['category'] ) ) {				_doing_it_wrong(					__METHOD__,					sprintf(						/* translators: %1$s: ability category slug, %2$s: ability name */						__( 'Ability category "%1$s" is not registered. Please register the ability category before assigning it to ability "%2$s".' ),						esc_html( $args['category'] ),						esc_html( $name )					),					'6.9.0'				);				return null;			}		} 		// The class is only used to instantiate the ability, and is not a property of the ability itself.		if ( isset( $args['ability_class'] ) && ! is_a( $args['ability_class'], WP_Ability::class, true ) ) {			_doing_it_wrong(				__METHOD__,				__( 'The ability args should provide a valid `ability_class` that extends WP_Ability.' ),				'6.9.0'			);

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 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/class-wp-abilities-registry.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.