Make WordPress Core


Ignore:
Timestamp:
06/29/2025 09:45:30 PM (14 months ago)
Author:
johnbillion
Message:

Role/Capability: Add support for passing a numerically-indexed array of capability names to the add_role() function and the WP_Roles::add_role() method.

Denying a capability at the point of creating the role is a relatively uncommon requirement, so this simplifies the syntax needed to grant a list of capabilities to the role.

Props eclev91, johnbillion, flixos90, johnjamesjacoby, soulseekah

Fixes #43421

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-roles.php

    r58975 r60364  
    144144         * Updates the list of roles, if the role doesn't already exist.
    145145         *
    146          * The capabilities are defined in the following format: `array( 'read' => true )`.
    147          * To explicitly deny the role a capability, set the value for that capability to false.
    148          *
    149          * @since 2.0.0
    150          *
    151          * @param string $role         Role name.
    152          * @param string $display_name Role display name.
    153          * @param bool[] $capabilities Optional. List of capabilities keyed by the capability name,
    154          *                             e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
    155          *                             Default empty array.
     146         * The list of capabilities can be passed either as a numerically indexed array of capability names, or an
     147         * associative array of boolean values keyed by the capability name. To explicitly deny the role a capability, set
     148         * the value for that capability to false.
     149         *
     150         * Examples:
     151         *
     152         *     // Add a role that can edit posts.
     153         *     wp_roles()->add_role( 'custom_role', 'Custom Role', array(
     154         *         'read',
     155         *         'edit_posts',
     156         *     ) );
     157         *
     158         * Or, using an associative array:
     159         *
     160         *     // Add a role that can edit posts but explicitly cannot not delete them.
     161         *     wp_roles()->add_role( 'custom_role', 'Custom Role', array(
     162         *         'read' => true,
     163         *         'edit_posts' => true,
     164         *         'delete_posts' => false,
     165         *     ) );
     166         *
     167         * @since 2.0.0
     168         * @since x.y.z Support was added for a numerically indexed array of strings for the capabilities array.
     169         *
     170         * @param string                               $role         Role name.
     171         * @param string                               $display_name Role display name.
     172         * @param array<string,bool>|array<int,string> $capabilities Capabilities to be added to the role.
     173         *                                                           Default empty array.
    156174         * @return WP_Role|void WP_Role object, if the role is added.
    157175         */
     
    159177                if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
    160178                        return;
     179                }
     180
     181                if ( wp_is_numeric_array( $capabilities ) ) {
     182                        $capabilities = array_fill_keys( $capabilities, true );
    161183                }
    162184
Note: See TracChangeset for help on using the changeset viewer.