WP_Roles
- Since
- 2.0.0
- Source
wp-includes/class-wp-roles.php:26
Core class used to implement a user roles API.
Description
The role option is simple, the structure is organized by role name that store the name in value of the 'name' key. The capabilities are stored as an array in the value of the 'capability' key.
array (
'rolename' => array (
'name' => 'rolename',
'capabilities' => array()
)
)Compatibility
- WordPress
- since 2.0.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Hooks and filters fired · 1
Every hook that fires from inside WP_Roles, in the order it appears in the class, grouped by the method that fires it.
WP_Roles::init_roles()
- do_action( wp_roles_init )action line 321
Properties · 6
$rolesarray[]public- List of roles and capabilities.
$role_objectsWP_Role[]public- List of the role objects.
$role_namesstring[]public- List of role names.
$role_keystringpublic- Option name for storing role list.
$use_dbboolpublic- Whether to use the database for retrieval and storage.
$site_idintprotected- The site ID the roles are initialized for.
Methods · 15
- __construct()Constructor.
- __call()Makes private/protected methods readable for backward compatibility.
- _init()Sets up the object properties.
- reinit()Reinitializes the object.
- add_role()Adds a role name with capabilities to the list.
- remove_role()Removes a role by name.
- add_cap()Adds a capability to role.
- remove_cap()Removes a capability from role.
- get_role()Retrieves a role object by name.
- get_names()Retrieves a list of role names.
- is_role()Determines whether a role name is currently in the list of available roles.
- init_roles()Initializes all of the available roles.
- for_site()Sets the site to operate on. Defaults to the current site.
- get_site_id()Gets the ID of the site for which roles are currently initialized.
- get_roles_data()Gets the available roles data.
Source code
#[AllowDynamicProperties]class WP_Roles { /** * List of roles and capabilities. * * @since 2.0.0 * @var array[] */ public $roles; /** * List of the role objects. * * @since 2.0.0 * @var WP_Role[] */ public $role_objects = array(); /** * List of role names. * * @since 2.0.0 * @var string[] */ public $role_names = array(); /** * Option name for storing role list. * * @since 2.0.0 * @var string */ public $role_key; /** * Whether to use the database for retrieval and storage. * * @since 2.1.0 * @var bool */ public $use_db = true; /** * The site ID the roles are initialized for. * * @since 4.9.0 * @var int */ protected $site_id = 0; /** * Constructor. * * @since 2.0.0 * @since 4.9.0 The `$site_id` argument was added. * * @global array $wp_user_roles Used to set the 'roles' property value. * * @param int $site_id Site ID to initialize roles for. Default is the current site. */ public function __construct( $site_id = null ) { global $wp_user_roles; $this->use_db = empty( $wp_user_roles ); $this->for_site( $site_id ); } /** * Makes private/protected methods readable for backward compatibility. * * @since 4.0.0 * * @param string $name Method to call. * @param array $arguments Arguments to pass when calling. * @return mixed|false Return value of the callback, false otherwise. */ public function __call( $name, $arguments ) { if ( '_init' === $name ) { return $this->_init( ...$arguments );Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-roles.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.