Do not allow users to create new posts and pages

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!

Topic user-roles capabilities users Wordpress

Category Web


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;
}

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.