Get Roles
// get roles
function get_editable_roles() {
global $wp_roles;
$all_roles = $wp_roles->roles;
$editable_roles = apply_filters('editable_roles', $all_roles);
return $editable_roles;
}Add Roles
function wporg_simple_role() {
add_role(
'simple_role',
'Simple Role',
array(
'read' => true,
'edit_posts' => true,
'upload_files' => true,
),
);
}
add_action( 'init', 'wporg_simple_role' );Remove Roles
// removing roles
function wporg_simple_role_remove() {
remove_role( 'simple_role' );
}
add_action( 'init', 'wporg_simple_role_remove' );Add Capabilities
// adding capabilities
function wporg_simple_role_caps() {
$role = get_role( 'simple_role' );
$role->add_cap( 'edit_others_posts', true ); // for removing capability remove_cap()
}
add_action( 'init', 'wporg_simple_role_caps' );Get Roles
// get role
get_role( $role );Check User Capacibility
// Check if a user have a specified role or capability with user_can().
user_can( $user, $capability );Check Current User Capacibility
// current_user_can() is a wrapper function for user_can() using the current user object as the $user parameter.
current_user_can( $capability );