get_role()WP 2.0.0

Retrieve an object with the capabilities of the specified role.

This function returns an instance of WP_Roles object, and through it, you can use the methods of the WP_Roles class.

Uses WP_Roles class, using the wp_roles() function.

1 time — 0.000068 sec (very fast) | 50000 times — 2.29 sec (fast)

No Hooks.

Return

WP_Role|null. WP_Role object if found, null if the role does not exist.

Usage

get_role( $role );
$role(string) (required)
Role name.

Examples

0

#1 Demonstration of work

$role = get_role( 'author' );
print_r($role);

Output:

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
		)

)
0

#2 Example of usage WP_Roles methods

The example below will add a new capability to the author role:

add_action( 'admin_init', 'add_theme_caps');
function add_theme_caps(){
	$role = get_role( 'author' );

	$role->add_cap( 'edit_others_posts' ); 
}

Changelog

Since 2.0.0 Introduced.

get_role() code WP 6.5.2

function get_role( $role ) {
	return wp_roles()->get_role( $role );
}