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/capabilities.php

    r60294 r60364  
    10971097 * Adds a role, if it does not exist.
    10981098 *
     1099 * The list of capabilities can be passed either as a numerically indexed array of capability names, or an
     1100 * associative array of boolean values keyed by the capability name. To explicitly deny the role a capability, set
     1101 * the value for that capability to false.
     1102 *
     1103 * Examples:
     1104 *
     1105 *     // Add a role that can edit posts.
     1106 *     add_role( 'custom_role', 'Custom Role', array(
     1107 *         'read',
     1108 *         'edit_posts',
     1109 *     ) );
     1110 *
     1111 * Or, using an associative array:
     1112 *
     1113 *     // Add a role that can edit posts but explicitly cannot not delete them.
     1114 *     add_role( 'custom_role', 'Custom Role', array(
     1115 *         'read' => true,
     1116 *         'edit_posts' => true,
     1117 *         'delete_posts' => false,
     1118 *     ) );
     1119 *
    10991120 * @since 2.0.0
    1100  *
    1101  * @param string $role         Role name.
    1102  * @param string $display_name Display name for role.
    1103  * @param bool[] $capabilities List of capabilities keyed by the capability name,
    1104  *                             e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
     1121 * @since x.y.z Support was added for a numerically indexed array of strings for the capabilities array.
     1122 *
     1123 * @param string                               $role         Role name.
     1124 * @param string                               $display_name Display name for role.
     1125 * @param array<string,bool>|array<int,string> $capabilities Capabilities to be added to the role.
     1126 *                                                           Default empty array.
    11051127 * @return WP_Role|void WP_Role object, if the role is added.
    11061128 */
Note: See TracChangeset for help on using the changeset viewer.