wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_get_abilities › WordPress Function

Since6.9.0
Deprecatedn/a
› wp_get_abilities ( $args = array() )
Parameters:
  • (array) $args { Optional. Arguments to filter the returned abilities. Default empty array. Omitted filters do not restrict the results. @type string $category Optional. Filter by category slug. Only abilities whose category exactly matches the given slug are included. @type string $namespace Optional. Filter by ability namespace prefix. Pass the namespace without a trailing slash, e.g. `'woocommerce'` matches `'woocommerce/create-order'`. @type array $meta Optional. Filter by meta key/value pairs. All keys must exist and non-array values must match strictly, including their types. Nested arrays are matched recursively as subsets, e.g. `array( 'mcp' => array( 'public' => true ) )`. @type bool $has_required_capabilities Optional. Whether to leave out abilities whose required capabilities the current user doesn't have. Abilities without required capabilities pass this check but remain subject to other filters. Permission callbacks do not run, so inclusion does not guarantee execution permission. @type callable $item_include_callback Optional. A callback invoked per ability after declarative filters. Receives a WP_Ability instance, returns bool. Return true to include, false to exclude. @type callable $result_callback Optional. A callback invoked once on the full matched array. Receives WP_Ability[], must return WP_Ability[]. Use for sorting, slicing, or reshaping the result. }
    Required: No
    Default: array()
See:
  • WP_Abilities_Registry::get_all_registered()
Returns:
  • (WP_Ability[]) An array of registered WP_Ability instances, keyed by ability name
    unless reshaped by a callback or filter. Returns an empty array if
    the registry is unavailable.
Defined at:
Codex:
Change Log:
  • 7.1.0
  • 7.2.0

Retrieves registered abilities, optionally filtered by the given arguments.

When called without arguments, returns all registered abilities. When called with an $args array, returns only abilities that match every specified condition. In both cases, the callbacks and filters below can change the result. Filtering pipeline (executed in order): 1. Declarative filters (category, namespace, meta, has_required_capabilities) — per-item, AND logic between arg types. 2. item_include_callback — per-item, caller-scoped. Return true to include, false to exclude. 3. wp_get_abilities_item_include filter — per-item, ecosystem-scoped. Plugins can enforce inclusion rules for abilities that passed the declarative filters. 4. result_callback — on the full matched array, caller-scoped. Sort, slice, or reshape. 5. wp_get_abilities_result filter — on the full array, ecosystem-scoped. Steps 1–3 run inside a single loop over the registry — no extra iteration. Examples: // All abilities. $abilities = wp_get_abilities(); // Filter by category. $abilities = wp_get_abilities( array( 'category' => 'content' ) ); // Filter by namespace. $abilities = wp_get_abilities( array( 'namespace' => 'woocommerce' ) ); // Filter by meta. $abilities = wp_get_abilities( array( 'meta' => array( 'show_in_rest' => true ) ) ); // Abilities whose declared minimum capabilities the current user has. // Input-dependent permission checks still run when executing an ability. $abilities = wp_get_abilities( array( 'has_required_capabilities' => true ) ); // Combine filters (AND logic between arg types). $abilities = wp_get_abilities( array( 'category' => 'content', 'namespace' => 'core', 'meta' => array( 'show_in_rest' => true ), ) ); // Caller-scoped per-item callback. $abilities = wp_get_abilities( array( 'item_include_callback' => function ( WP_Ability $ability ) { return current_user_can( 'manage_options' ); }, ) ); // Caller-scoped result callback (sort + paginate). $abilities = wp_get_abilities( array( 'result_callback' => function ( array $abilities ) { uasort( $abilities, fn( $a, $b ) => strcasecmp( $a->get_label(), $b->get_label() ) ); return array_slice( $abilities, 0, 10, true ); }, ) ); The pipeline always runs, even when called with no arguments. This ensures that the wp_get_abilities_result filter always fires, giving plugins a reliable place to enforce universal shaping rules. For raw, unfiltered registry data that bypasses the filter pipeline entirely, use WP_Abilities_Registry::get_all_registered directly.


Source

function wp_get_abilities( array $args = array() ): array {
	$registry = WP_Abilities_Registry::get_instance();
	if ( null === $registry ) {
		return array();
	}

	$abilities = $registry->get_all_registered();

	$category                  = isset( $args['category'] ) && is_string( $args['category'] ) ? $args['category'] : '';
	$namespace                 = isset( $args['namespace'] ) && is_string( $args['namespace'] ) ? rtrim( $args['namespace'], '/' ) . '/' : '';
	$meta                      = isset( $args['meta'] ) && is_array( $args['meta'] ) ? $args['meta'] : array();
	$has_required_capabilities = ! empty( $args['has_required_capabilities'] );
	$item_include_callback     = isset( $args['item_include_callback'] ) && is_callable( $args['item_include_callback'] ) ? $args['item_include_callback'] : null;
	$result_callback           = isset( $args['result_callback'] ) && is_callable( $args['result_callback'] ) ? $args['result_callback'] : null;

	$matched = array();

	foreach ( $abilities as $name => $ability ) {
		// Step 1a: Filter by category.
		if ( '' !== $category && $ability->get_category() !== $category ) {
			continue;
		}

		// Step 1b: Filter by namespace prefix.
		if ( '' !== $namespace && ! str_starts_with( $ability->get_name(), $namespace ) ) {
			continue;
		}

		// Step 1c: Filter by meta key/value pairs (AND logic, supports nested arrays).
		if ( ! empty( $meta ) && ! _wp_get_abilities_match_meta( $ability->get_meta(), $meta ) ) {
			continue;
		}

		// Step 1d: Leave out abilities whose required capabilities the current user doesn't have.
		if ( $has_required_capabilities ) {
			foreach ( $ability->get_required_capabilities() as $capability ) {
				if ( ! current_user_can( $capability ) ) {
					continue 2;
				}
			}
		}

		// Step 2: Caller-scoped per-item callback.
		$include = true;
		if ( null !== $item_include_callback ) {
			$include = (bool) call_user_func( $item_include_callback, $ability );
		}

		/**
		 * Filters whether an individual ability should be included in the result set.
		 *
		 * Fires after the declarative filters and the caller-scoped item_include_callback.
		 * Only abilities that pass the declarative filters reach this hook. It can override
		 * the caller-scoped callback's result.
		 *
		 * @since 7.1.0
		 *
		 * @param bool       $include The caller-scoped callback's result, or true when no callback is provided.
		 * @param WP_Ability $ability The ability instance being evaluated.
		 * @param array      $args    The full $args array passed to wp_get_abilities().
		 */
		$include = (bool) apply_filters( 'wp_get_abilities_item_include', $include, $ability, $args );

		if ( $include ) {
			$matched[ $name ] = $ability;
		}
	}

	// Step 4: Caller-scoped result callback.
	if ( null !== $result_callback ) {
		$matched = (array) call_user_func( $result_callback, $matched );
	}

	/**
	 * Filters the full list of matched abilities after all per-item filtering is complete.
	 *
	 * Fires after the caller-scoped result_callback. Plugins can use this to sort,
	 * paginate, or reshape the final result set universally.
	 *
	 * @since 7.1.0
	 *
	 * @param WP_Ability[] $matched The matched abilities after all filtering.
	 * @param array        $args    The full $args array passed to wp_get_abilities().
	 */
	return (array) apply_filters( 'wp_get_abilities_result', $matched, $args );
}