Adding Custom Roles
add_role() is used to add a custom role
- to add custom role with same capability as admin but not allow to add edit update delete post we can:
- get all capabilties for admin
- from that remove capability for add edit update delete posts
- create a role and give the modified capability to them.
function add_custom_role(){
$capabilities = get_role['administrator']->capabilities;
unset($capabilities['publish_posts']);
unset($capabilities['edit_posts']);
unset($capabilities['delete_posts']);
add_role('cannot_manager', 'Cannot Manager', $capabilities);
}
Adding Custom Capability
- we can filter hook into the default capabilities and append our custom capability to it
function simple_role_caps() {
// Gets the simple_role role object.
$role = get_role( 'simple_role' );
// Add a new capability.
$role->add_cap( 'access_admin_menu', true );
}
// Add simple_role capabilities, priority must be after the initial role definition.
add_action( 'init', 'simple_role_caps', 11 );
- then on admin menu page check :
if (! current_user_can( 'access_admin_menu' ) ) {
die("you Don't have access to this page");
}
map_meta_cap() function
- It is used to map meta capabilties to primitive capability
- eg.
edit_posts is a primitive capability, we want to add capability like edit_rt_movie which is a meta capabiltiy
map_meta_cap filter:
- when map_meta_cap() is called for capability check, it returns the required capabilties.
- This is where the map_meta_cap filter comes into action, we can use it to set our custom capability.
function custom_map_meta_cap($caps, $cap){
if('edit_rt_movie' === $cap){
$caps['edit_post'] = true;
}
return $caps;
}
add_filter('map_meta_cap', 'custom_map_meta_cap',10,2);
Leave a Reply