WP_Roles{}
The class lets you manage roles and role capabilities in WordPress.
Role options are simple. The structure is organized by role name, which is the key of the main array. A role's capabilities are in a nested array with two parameters: name and capabilities:
[ 'rolename' => [ 'name' => 'rolename', 'capabilities' => [] ] ]
Works together with WP_Role, which creates objects for the $this->role_objects property.
Roles and capabilities are not generated continuously; they are stored in the wp_user_roles option.
To get the global instance of this class, always use the wp_roles() helper function.
Hooks from the class
Usage
global $wp_roles; $wp_roles = new WP_Roles(); // or simply $wp_roles = wp_roles();
Class properties
- $roles(array)
List of roles and capabilities. Similar to the
$role_objectsproperty and has the following structure:[ 'rolename' => [ 'name' => 'rolename', 'capabilities' => [] ] ]
- $role_objects(array)
List of role objects. Has the following structure:
[ 'rolename' => [ 'name' => 'rolename', 'capabilities' => [] ] ]
- $role_names(array)
List of role names only:
[role_names] => [ [administrator] => Administrator [editor] => Editor [author] => Author [contributor] => Contributor [subscriber] => Subscriber ]
- $role_key(string)
- The name of the option that contains the role list, for example: 'wp_user_roles'.
- $use_db(bool)
- Whether to use the database to retrieve and store roles.
Methods
- __construct()(public)
- Constructor. Calls
_init().
Returns: nothing - __call(
$name,$arguments) (public) - For backward compatibility, so non-existent methods work.
Returns: false/mixed - _init()(protected)
Sets object properties. Uses the globals:
$wpdb,$wp_user_roles.If the global
$wp_user_rolesis not set, the"{$wpdb->prefix}user_roles"expression is used as the key.
Returns: nothing- reinit() (public)
- Reinitializes the object. Usually called by
switch_to_blog()after it switches wpdb to another site in a Multisite installation.
Returns: nothing - add_role(
$role,$display_name,$capabilities=[]) (public) - Adds a role with capabilities to the list. Updates the role list if the role does not already exist.
Capabilities are defined in this format:[ 'read' => true ];
To deny the specified capability, set its value to false:[ 'read' => false ];
This method has a wrapper function: add_role().
Returns: WP_Role|nothing. WP_Role if the role was added. - remove_role(
$role) (public) - Removes a role by name. This method has a wrapper function: remove_role().
Returns: nothing - add_cap(
$role,$cap,$grant= true ) (public) - Adds the
$capcapability to the role specified in$role. The boolean$grantdetermines whether to grant or deny the capability. See add_cap() for details.
Returns: nothing - remove_cap(
$role,$cap) (public) - Removes the
$capcapability from the role specified in$role.
Returns: nothing - get_role(
$role) (public) - Retrieves the specified role object.
Returns: a WP_Role object if the role is found, or null if it does not exist. - get_names()(public)
- Retrieves an array of all role names.
Returns: an array of names - is_role(
$role) (public) - Checks whether the specified role exists in the current role list.
Returns: true|false
Examples
#1 What the roles array contains
print_r( wp_roles() ); /* WP_Roles Object ( [roles] => Array ( [administrator] => Array ( [name] => Administrator [capabilities] => Array ( [switch_themes] => 1 [edit_themes] => 1 [activate_plugins] => 1 [edit_plugins] => 1 [edit_users] => 1 [edit_files] => 1 [manage_options] => 1 [moderate_comments] => 1 [manage_categories] => 1 [manage_links] => 1 [upload_files] => 1 [import] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_others_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_10] => 1 [level_9] => 1 [level_8] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_others_pages] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [delete_pages] => 1 [delete_others_pages] => 1 [delete_published_pages] => 1 [delete_posts] => 1 [delete_others_posts] => 1 [delete_published_posts] => 1 [delete_private_posts] => 1 [edit_private_posts] => 1 [read_private_posts] => 1 [delete_private_pages] => 1 [edit_private_pages] => 1 [read_private_pages] => 1 [delete_users] => 1 [create_users] => 1 [unfiltered_upload] => 1 [edit_dashboard] => 1 [update_plugins] => 1 [delete_plugins] => 1 [install_plugins] => 1 [update_themes] => 1 [install_themes] => 1 [manage_downloads] => 1 [manage_database] => 1 [democracy_admin] => 1 [update_core] => 1 [list_users] => 1 [remove_users] => 1 [add_users] => 1 [promote_users] => 1 [edit_theme_options] => 1 [delete_themes] => 1 [export] => 1 ) ) [editor] => Array ( [name] => Editor [capabilities] => Array ( [moderate_comments] => 1 [manage_categories] => 1 [manage_links] => 1 [upload_files] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_others_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_others_pages] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [delete_pages] => 1 [delete_others_pages] => 1 [delete_published_pages] => 1 [delete_posts] => 1 [delete_others_posts] => 1 [delete_published_posts] => 1 [delete_private_posts] => 1 [edit_private_posts] => 1 [read_private_posts] => 1 [delete_private_pages] => 1 [edit_private_pages] => 1 [read_private_pages] => 1 [democracy_admin] => 1 ) ) [author] => Array ( [name] => Author [capabilities] => Array ( [upload_files] => 1 [edit_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [read] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [delete_posts] => 1 [delete_published_posts] => 1 ) ) [contributor] => Array ( [name] => Contributor [capabilities] => Array ( [edit_posts] => 1 [read] => 1 [level_1] => 1 [level_0] => 1 [delete_posts] => 1 ) ) [subscriber] => Array ( [name] => Subscriber [capabilities] => Array ( [read] => 1 [level_0] => 1 ) ) ) [role_objects] => Array ( [administrator] => WP_Role Object ( [name] => administrator [capabilities] => Array ( [switch_themes] => 1 [edit_themes] => 1 [activate_plugins] => 1 [edit_plugins] => 1 [edit_users] => 1 [edit_files] => 1 [manage_options] => 1 [moderate_comments] => 1 [manage_categories] => 1 [manage_links] => 1 [upload_files] => 1 [import] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_others_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_10] => 1 [level_9] => 1 [level_8] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_others_pages] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [delete_pages] => 1 [delete_others_pages] => 1 [delete_published_pages] => 1 [delete_posts] => 1 [delete_others_posts] => 1 [delete_published_posts] => 1 [delete_private_posts] => 1 [edit_private_posts] => 1 [read_private_posts] => 1 [delete_private_pages] => 1 [edit_private_pages] => 1 [read_private_pages] => 1 [delete_users] => 1 [create_users] => 1 [unfiltered_upload] => 1 [edit_dashboard] => 1 [update_plugins] => 1 [delete_plugins] => 1 [install_plugins] => 1 [update_themes] => 1 [install_themes] => 1 [manage_downloads] => 1 [manage_database] => 1 [democracy_admin] => 1 [update_core] => 1 [list_users] => 1 [remove_users] => 1 [add_users] => 1 [promote_users] => 1 [edit_theme_options] => 1 [delete_themes] => 1 [export] => 1 ) ) [editor] => WP_Role Object ( [name] => editor [capabilities] => Array ( [moderate_comments] => 1 [manage_categories] => 1 [manage_links] => 1 [upload_files] => 1 [unfiltered_html] => 1 [edit_posts] => 1 [edit_others_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [edit_pages] => 1 [read] => 1 [level_7] => 1 [level_6] => 1 [level_5] => 1 [level_4] => 1 [level_3] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [edit_others_pages] => 1 [edit_published_pages] => 1 [publish_pages] => 1 [delete_pages] => 1 [delete_others_pages] => 1 [delete_published_pages] => 1 [delete_posts] => 1 [delete_others_posts] => 1 [delete_published_posts] => 1 [delete_private_posts] => 1 [edit_private_posts] => 1 [read_private_posts] => 1 [delete_private_pages] => 1 [edit_private_pages] => 1 [read_private_pages] => 1 [democracy_admin] => 1 ) ) [author] => WP_Role Object ( [name] => author [capabilities] => Array ( [upload_files] => 1 [edit_posts] => 1 [edit_published_posts] => 1 [publish_posts] => 1 [read] => 1 [level_2] => 1 [level_1] => 1 [level_0] => 1 [delete_posts] => 1 [delete_published_posts] => 1 ) ) [contributor] => WP_Role Object ( [name] => contributor [capabilities] => Array ( [edit_posts] => 1 [read] => 1 [level_1] => 1 [level_0] => 1 [delete_posts] => 1 ) ) [subscriber] => WP_Role Object ( [name] => subscriber [capabilities] => Array ( [read] => 1 [level_0] => 1 ) ) ) [role_names] => Array ( [administrator] => Administrator [editor] => Editor [author] => Author [contributor] => Contributor [subscriber] => Subscriber ) [role_key] => wp_user_roles [use_db] => 1 ) */
#2 Create a new role: "Basic contributor":
$result = add_role(
'basic_contributor', 'Basic contributor',
[
'read' => true, // true grants this capability
'edit_posts' => true, // true allows editing posts
'delete_posts' => false, // false prevents deleting posts
]
);
if ( null !== $result ) {
echo 'Hooray! The new role was created!';
}
else {
echo 'Oops... This role already exists.';
}
#3 Remove the "subscriber" role:
register_deactivation_hook( __FILE__, function(){
remove_role( 'subscriber' );
} );Changelog
| Since 2.0.0 | Introduced. |