WordPress at a glance

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.

Works based on: wp_roles(), WP_Roles()
✈ 1 time = 0.000068s = very fast | 50000 times = 2.29s = 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

#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
		)

)

#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.

Code of get role: wp-includes/capabilities.php WP 5.2.2

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

Related Functions

From tag: Roles capabilities

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register