I’m currently developing a WordPress business directory plugin with a ‘business’ custom post type. I have it creating a custom ‘business_author’ user role on activation and assigning specific capabilities to that role but it seems to be restricting the ‘Administrator’ role from accessing the ‘business’ custom post type. I’ve tried adding the capabilities to the ‘Administrator’ role when the plugin is activated but it doesn’t seem to work.
p.s I’m using the ‘map_meta_cap’ filter to create custom capabilities for this post type.
Code example below…
register_activation_hook( __FILE__, 'tis_business_activation' );
register_deactivation_hook( __FILE__, 'tis_business_deactivation' );
function tis_business_activation() {
add_role('business_author', 'Business Listing Author', array(
'publish_businesses' => false,
'edit_businesses' => true,
'edit_others_busineses' => false,
'delete_businesses' => true,
'delete_others_businesess' => false,
'read_private_businesses' => false,
'edit_business' => true,
'delete_business' => true,
'read_business' => true,
'read' => true,
'upload_files' => true
));
//Attempting to add above capabilities to administrator role.
$administrator = get_role('administrator');
$administrator->add_cap('publish_business');
$administrator->add_cap('edit_business');
$administrator->add_cap('edit_others_businesses');
$administrator->add_cap('delete_businesses');
$administrator->add_cap('delete_others_businesses');
$administrator->add_cap('read_private_businesses');
$administrator->add_cap('edit_business');
$administrator->add_cap('delete_business');
$administrator->add_cap('read_business');
}
function tis_business_deactivation() {
remove_role('business_author');
global $wp_roles;
$wp_roles->remove_cap('administrator', 'publish_business');
$wp_roles->remove_cap('administrator', 'edit_business');
$wp_roles->remove_cap('administrator', 'edit_others_businesses');
$wp_roles->remove_cap('administrator', 'delete_businesses');
$wp_roles->remove_cap('administrator', 'delete_others_businesses');
$wp_roles->remove_cap('administrator', 'read_private_businesses');
$wp_roles->remove_cap('administrator', 'edit_business');
$wp_roles->remove_cap('administrator', 'delete_business');
$wp_roles->remove_cap('administrator', 'read_business');
}
Any help would be amazing I’m really stuck with this one.
Sorry if this has already been answered, I searched high and low for an answer but couldn’t find one.
1 Answer
Referring to the Codex entry for this, I suggest using another hook. They use the ‘admin_init’ action:
function add_theme_caps() {
$role = get_role( 'author' ); // gets the author role
$role->add_cap( 'edit_others_posts' ); // would allow the author to edit others' posts for current theme only
}
add_action( 'admin_init', 'add_theme_caps');