I need to create a custom user role who will be able to create posts (of a specific post type) but not publish them (just like the current contributor role.
I’m being a bit confused on how to create a new role or capability to achieve this. How/where do I start?
Thanks
UPDATE:
I added these to the custom post function as suggested by Eric Holmes
'capabilities' => array(
'edit_posts' => 'edit_helps',
'edit_post' => 'edit_help',
'read_post' => 'read_helps',
),
Added these to plugin activation hook, and the opposite in deactivation hook (I’m modifying the contributor role itself):
function modify_user_capabilities() {
$role = get_role( 'contributor' );
$role->remove_cap( 'delete_posts' );
$role->remove_cap( 'edit_posts' );
$role->add_cap('edit_helps');
$role->add_cap('edit_help');
$role->add_cap('read_helps');
}
register_activation_hook( __FILE__, 'modify_user_capabilities' );
Now only the contributor can edit this post type, not other users (Eg admin)
Is there a better way to batch assign them these capabilities? I edited my activation hook to:
function modify_user_capabilities() {
$role = get_role( 'contributor' );
$role->remove_cap( 'delete_posts' );
$role->remove_cap( 'edit_posts' );
foreach (array('administrator', 'editor', 'author', 'contributor') as $user_role) {
$role = get_role($user_role);
$role->add_cap('edit_helps');
$role->add_cap('edit_help');
$role->add_cap('read_helps');
}
}
UPDATE 2:
I totally forgot to assign someone to delete the posts. So I updated the function:
function modify_user_capabilities() {
//remove the contributor from editing any post
$role = get_role( 'contributor' );
$role->remove_cap( 'delete_posts' );
$role->remove_cap( 'edit_posts' );
foreach (array('administrator', 'editor', 'author', 'contributor') as $user_role) {
$role = get_role($user_role);
$role->add_cap('edit_helps');
$role->add_cap('edit_help');
$role->add_cap('read_helps');
}
//let admins delete posts
$role = get_role('administrator');
$role->add_cap('delete_helps');
$role->add_cap('delete_publised_helps');
$role->add_cap('delete_others_helps');
$role->add_cap('delete_help');
}
Now the admin can delete these posts.