Access all of Typerocket. Get Pro.
Roles & Capabilities
( v6 )
- # What Are Roles?
- # List All
- # List Capabilities
- # Get Editable Roles
- # Add Role
- # Remove Role
- # Role Exists
- # Get Role
- # Update Role Capabilities
- # Custom Post Type Capabilities
- # Custom Taxonomy Capabilities
What Are Roles?
The Roles
object has several methods for making working with roles and capabilities. The object gives you enhanced APIs for working with WordPress roles and capabilities.
List All
Get a list of all roles and their capabilities.
tr_roles()->all();
List Capabilities
Get a list of all capabilities and their asigned roles.
tr_roles()->capabilities();
Get Editable Roles
Fetch a filtered list of user roles that the current user is allowed to edit.
tr_roles()->getEditableRoles();
Add Role
Add a new role and assign capabilities to that role.
tr_roles()->add(
'typerocket_admin',
['edit_posts' => true, 'delete_posts' => false],
'TypeRocket Admin'
);
Remove Role
Remove a non-guarded role. Guarded roles include: administrator
, editor
, author
, subscriber
, contributor
, and super_admin
(multi-site).
tr_roles()->remove('typerocket_admin');
Role Exists
Check if role exists. Returns true
or false
.
tr_roles()->exists('typerocket_admin');
Get Role
Get the native WordPress WP_Role
object of a role.
tr_roles()->get('typerocket_admin');
Update Role Capabilities
Add or remove a role's capabilities. To add capabilities:
$capabilities = [
'read',
'activate_plugins'
];
tr_roles()->updateRolesCapabilities('typerocket_admin', $capabilities);
To remove capabilities:
$capabilities = [
'read',
'activate_plugins'
];
$remove = true;
tr_roles()->updateRolesCapabilities('typerocket_admin', $capabilities, $remove);
Custom Post Type Capabilities
When registering a post type with TypeRocket using tr_post_type()
with custom capabilities you will need to add the custom capabilities to the role you want to have access to it. The getCustomPostTypeCapabilities()
method allows you to access the list of capabilities you need to add to a role.
// Register Post Type
$books = tr_post_type('Book', 'Books');
$books->customCapabilities();
// Add Custom Capabilities
$cap = tr_roles()->getCustomPostTypeCapabilities('book', 'books');
tr_roles()->updateRolesCapabilities('administrator', $cap);
Custom Taxonomy Capabilities
When registering a taxonomy with TypeRocket using tr_taxonomy()
with custom capabilities you will need to add the custom capabilities to the role you want to have access to it. The getTaxonomyCapabilities()
method allows you to access the list of capabilities you need to add to a role.
// Register Taxonomy
$pub = tr_taxonomy('Publisher');
$pub->customCapabilities();
// Add Custom Capabilities
$caps = tr_roles()->getTaxonomyCapabilities('publisher', 'publishers');
tr_roles()->updateRolesCapabilities('administrator', $cap);
Found a typo? Something is wrong in this documentation? Fork and edit it!