How to stop contributors editing post type but allowing them to edit a custom post type?

I need to stop ‘contributors’ from editing the standard post type, but I want to allow them to edit a custom post type. My code so far:

remove_cap( 'contributor', 'edit_posts' );

Thanks!

UPDATE: Why the above answer isn’t useful to me.
I am looking for a coding solution not a plugin recommendation. I am also using a standard role ‘contributor’ not creating my own role. Thanks!

1 Answer
1

Add the capabilities to the role.
Replace ‘cpt’ with the name of your custom post type:

$role = get_role( 'contributor' );
$role->add_cap( 'delete_published_cpt' );
$role->add_cap( 'delete_others_cpt' );
$role->add_cap( 'delete_cpt' );
$role->add_cap( 'edit_others_cpt' );
$role->add_cap( 'edit_published_cpt' );
$role->add_cap( 'edit_cpt' );
$role->add_cap( 'publish_cpt' );

But you shouldn’t add or remove caps on every page load.
That’s why a plugin is better.
You adjust caps on the plugin activation using register_activation_hook. And you can revert your changes using register_deactivation_hook because you have a conscience.

Leave a Comment