The get_role() WordPress PHP function retrieves the role object for a given role name.
Usage
get_role( $role )
Example:
$editor_role = get_role('editor');
Parameters
$role
(string) (Required): Role name.
More information
See WordPress Developer Resources: get_role()
Examples
Retrieve Editor Role Capabilities
Get the capabilities of the ‘editor’ role.
$editor_role = get_role('editor'); $capabilities = $editor_role->capabilities;
Add a Custom Capability to Author Role
Add a new capability called ‘manage_ads’ to the ‘author’ role.
$author_role = get_role('author'); $author_role->add_cap('manage_ads');
Remove a Capability from Contributor Role
Remove the ‘edit_published_posts’ capability from the ‘contributor’ role.
$contributor_role = get_role('contributor'); $contributor_role->remove_cap('edit_published_posts');
Check if Administrator Role has a Specific Capability
Check if the ‘administrator’ role has the ‘manage_options’ capability.
$admin_role = get_role('administrator'); if (isset($admin_role->capabilities['manage_options'])) { echo "Administrator can manage options."; } else { echo "Administrator cannot manage options."; }
List All Capabilities for a Custom Role
Display all capabilities for a custom role named ‘custom_role’.
$custom_role = get_role('custom_role'); foreach ($custom_role->capabilities as $capability => $value) { echo "{$capability}<br>"; }