Make WordPress Core


Ignore:
Timestamp:
08/24/2026 07:42:56 AM (4 weeks ago)
Author:
gziolo
Message:

Make the ability category optional

When the category argument is omitted during ability registration, the registry now assigns the built-in uncategorized category. This category is registered alongside the other core ability categories.

Previously, the registry only validated a category when one was provided, but WP_Ability still required the argument, so a registration without a category could never succeed. The fallback is applied before validation, so invalid values are still rejected. This includes non-string types and empty or unregistered category slugs.

Also updates the public API documentation and adds regression coverage for the fallback and for invalid category values.

Props hokansardar, sachinrajcp123, jorgefilipecosta, gziolo.
Fixes #65569.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/abilities-api.php

    r62737 r63343  
    129129 *     add_action( 'wp_abilities_api_init', 'my_plugin_register_abilities' );
    130130 *
     131 * On failure, this function returns `null` and calls `_doing_it_wrong()` with the reason.
     132 * By default, the resulting notice is displayed when `WP_DEBUG` is enabled.
     133 *
    131134 * ### Naming Conventions
    132135 *
     
    139142 * ### Categories
    140143 *
    141  * Abilities must be organized into categories. Ability categories provide better
    142  * discoverability and must be registered before the abilities that reference them:
     144 * Abilities can be organized into categories. If no category is provided, the ability is
     145 * assigned to the built-in `uncategorized` category. Custom categories must be registered
     146 * before the abilities that reference them:
    143147 *
    144148 *     function my_plugin_register_categories(): void {
     
    229233 *
    230234 * @since 6.9.0
     235 * @since 7.2.0 The `category` argument is now optional and defaults to `uncategorized`.
    231236 *
    232237 * @see WP_Abilities_Registry::register()
     
    243248 *     @type string               $description         Required. A detailed description of what the ability does
    244249 *                                                     and when it should be used.
    245  *     @type string               $category            Required. The ability category slug this ability belongs to.
    246  *                                                     The ability category must be registered via `wp_register_ability_category()`
    247  *                                                     before registering the ability.
     250 *     @type string               $category            Optional. The ability category slug this ability belongs to.
     251 *                                                     Defaults to `uncategorized`. Custom categories must be registered
     252 *                                                     via `wp_register_ability_category()` before registering the ability.
    248253 *     @type callable             $execute_callback    Required. A callback function to execute when the ability is invoked.
    249254 *                                                     Receives optional mixed input data and must return either a result
     
    608613 *
    609614 * Ability categories provide a way to organize and group related abilities for better
    610  * discoverability and management. Ability categories must be registered before abilities
    611  * that reference them.
     615 * discoverability and management. Custom categories must be registered before abilities
     616 * that reference them. Abilities that omit a category are assigned to the built-in
     617 * `uncategorized` category, which is intended as an escape hatch for simple or transitional
     618 * registrations.
    612619 *
    613620 * Ability categories must be registered on the `wp_abilities_api_categories_init` action hook.
     
    625632 *     }
    626633 *     add_action( 'wp_abilities_api_categories_init', 'my_plugin_register_categories' );
     634 *
     635 * On failure, this function returns `null` and calls `_doing_it_wrong()` with the reason.
     636 * By default, the resulting notice is displayed when `WP_DEBUG` is enabled.
    627637 *
    628638 * @since 6.9.0
Note: See TracChangeset for help on using the changeset viewer.