Check the below code-
add_action( 'admin_init', 'the_dramatist_add_role_caps', 999 );
function the_dramatist_add_role_caps() {
// Add the roles you'd like to administer the custom post types
$roles = array( 'editor', 'administrator' );
// Loop through each role and assign capabilities
foreach($roles as $the_role) {
$role = get_role($the_role);
$role->add_cap( 'read' );
$role->add_cap( 'read_{Post_Type_A_Name}' ); // Also do for Post_Type_B
$role->add_cap( 'read_private_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'edit_{Post_Type_A_Name}' ); // Also do for Post_Type_B
$role->add_cap( 'edit_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'edit_others_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'edit_published_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'publish_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'delete_others_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'delete_private_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
$role->add_cap( 'delete_published_{Post_Type_A_Name}s' ); // Also do for Post_Type_B
}
}
This way you can add permission to any user roles.
If you wanna create a new role for the user who should get permission then create a new role like below-
function add_the_dramatist_role() {
add_role('the_dramatist_role',
'The Dramatist Role',
array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'publish_posts' => false,
'upload_files' => true,
)
);
}
// Remember this must be called plugin main file.
register_activation_hook( __FILE__, 'add_the_dramatist_role' );
Then add the_dramatist_role
to the previous function the_dramatist_add_role_caps
‘s $roles
variable like this-
$roles = array( 'the_dramatist_role', 'editor', 'administrator' );
Now the_dramatist_role
will have all those privileges.
Hope the above helps.