4

I'm defining a new user role called Proofreader and as the name says, users of this group should be able to read and also edit the posts and pages. But I do not want them to create new ones.

With my custom post types I can set a capability create_post -> create_{cpt_name} and set it to false for this user group. In the backend they now don't see the add button, but can still edit the posts. That's exactly what I want. Unfortunately I was unable to find a way to set this capability for the predefined CPT of WordPress (Posts & Pages). Also the Members plugin seems to be unable to restrict user roles of creating new posts while letting them edit.

While researching, I found, that this seems to be a big issue of WordPress which is not solved yet. Is this true?

If not, I would be really thankful if anyone could help me out with this... It would really be a solution of a big problem of my website!

2 Answers 2

0

Use remove_cap for this.

function remove_proofreader_create_posts(){
    global $wp_roles;
    $wp_roles->remove_cap( 'proof_reader', 'create_posts' );
    $wp_roles->remove_cap( 'proof_reader', 'create_pages' );
}

NOTE: This is not a global function, but a method of the WP_Roles, WP_Role and WP_User classes. It must be called using an instance of one of these classes, as shown in the examples.

ALSO: You'll want to call the function once, as in during plugin activation, not on a constant hook.

Reference: https://codex.wordpress.org/Function_Reference/remove_cap

4
  • Sorry for answering so late, I was busy... I don't know why, but it does not work...
    – Sam
    Feb 10, 2017 at 23:16
  • Did you add the function with an add action? Feb 11, 2017 at 3:45
  • Yes of course. For testing reasons I added it on init
    – Sam
    Feb 11, 2017 at 14:51
  • Just to check, that none of the proofreaders are also editors or something else that has the capability. Feb 12, 2017 at 13:08
0

Maybe this alternate approach to remove_cap() will do the trick

add_action( 'init', 'stackx_set_proof_reader_capabilities' );
function stackx_set_proof_reader_capabilities() {

   // Get the role object.
   $role = get_role( 'proof_reader' );

   // A list of capabilities to remove from editors.
   $caps = array(
       'creat_posts',
       'create_pages',
   );

   foreach ( $caps as $cap ) :
       // Remove the capability.
       $role->remove_cap( $cap );
   endforeach;
}
2
  • 1
    That doesn't work either :( Are you sure, that it's possible to remove that capability for the built in post types? When registering a CPT, you can add the create_posts cap and set it to create_{cpt_name}. That works well in my case, but this seems to be ignored...
    – Sam
    Feb 12, 2017 at 20:58
  • I cannot say that I have ever removed this specific set before, but I can't think of any reason it shouldn't go like any other. I will spin up a test site this evening and confirm. Feb 12, 2017 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.