I am creating custom plugin in which I would like to add capabilities as per User role for particular custom post type dynamically.
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'elementor-siteset' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $this->slug ),
'capability_type' => 'member',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title'),
'menu_icon' => 'dashicons-groups',
'exclude_from_search' => false,
'can_export' => true,
'capabilities' => array(
'edit_post' => 'edit_member',
'edit_posts' => 'edit_members',
'edit_others_posts' => 'edit_other_members',
'publish_posts' => 'edit_members',
'read_post' => 'read_member',
'read_private_posts' => 'read_private_members',
'delete_post' => 'delete_member',
'create_posts' => 'edit_members'
),
'map_meta_cap' => true
);
This is my WordPress backend.
I want to make it dynamically. If I save from my backed then it should save the capabilities for the particular role for that particular CPT. I will resolve this once I got solution of following thing.
I want to do this. Following code has been added only for the testing. I would like to add only read, edit and delete capabilities for member CPT for particular role. How can I achieve that?
function add_theme_caps() {
// gets the administrator role
$admins = get_role( 'editor' );
$admins->add_cap( 'edit_member' );
$admins->add_cap( 'edit_members' );
$admins->add_cap( 'edit_other_members' );
$admins->add_cap( 'publish_members' );
$admins->add_cap( 'delete_member' );
$admins->add_cap( 'read_member' );
$admins->add_cap( 'read_private_members' );
}
add_action( 'admin_init', 'add_theme_caps', 10,2);
Above code is removing whole CPT "Member" for particular role. If I add remove_cap()
to remove any of the capabilities then it will not affect as expected.