0

I need to stop any site user who is a 'contributor' from editing the standard posts, but I want them to be able to edit a custom post type called courses. Here is my code that I have so far, it is not working: Activation of theme:

   function remove_editor_read_private_posts(){
 global $wp_roles;
 $wp_roles->remove_cap( 'contributor', 'edit_post' );
}

add_action( 'after_setup_theme', 'remove_editor_read_private_posts' ); 

The above code needs to remove the ability to edit standard posts, but allow contributors to edit a custom post type called courses. Before this code contributors can edit both standard and the custom post type called courses.

Deactivation of theme:

 function remove_editor_read_private_poststwo(){
     global $wp_roles;
     $wp_roles->add_cap( 'contributor', 'edit_post' );
    }

     add_action( 'switch_theme', 'remove_editor_read_private_poststwo' ); 
2

Hint

The changes on roles and no layout-topics should not a part of a Theme, is much more a topic for a plugin. maybe you think about this topic on point like maintenance, switch of theme and hooks for activation - easier on plugins.

For your problem

You must add the roles new capabilities for your custom post type and remove the capabilities for type post. On activation change the roles for the follow capabilities.

foreach ( self::$todo_roles as $role ) {            
     $wp_roles->add_cap( $role, 'edit_' . self::$post_type_1 );
     $wp_roles->add_cap( $role, 'read_' . self::$post_type_1 ); 
     $wp_roles->add_cap( $role, 'delete_' . self::$post_type_1 );
     $wp_roles->add_cap( $role, 'edit_' . self::$post_type_1 . 's' );
     $wp_roles->add_cap( $role, 'edit_others_' . self::$post_type_1 . 's' );    
     $wp_roles->add_cap( $role, 'publish_' . self::$post_type_1 . 's' );
     $wp_roles->add_cap( $role, 'read_private_' . self::$post_type_1 . 's' );   
     $wp_roles->add_cap( $role, 'delete_' . self::$post_type_1 . 's' ); 
     $wp_roles->add_cap( $role, 'delete_private_' . self::$post_type_1 . 's' ); 
     $wp_roles->add_cap( $role, 'delete_published_' . self::$post_type_1 . 's' );
     $wp_roles->add_cap( $role, 'delete_others_' . self::$post_type_1 . 's' );  
     $wp_roles->add_cap( $role, 'edit_private_' . self::$post_type_1 . 's' );
     $wp_roles->add_cap( $role, 'edit_published_' . self::$post_type_1 . 's' ); 
} 

With something like the above you can add capabilities the roles.

You should also do this to remove all rights for the type post.

$wp_roles->remove_cap( ... )

As hint to the source. The self::$post_type_1 is a variable in a class for the custom post type. The foreach loop is to add this capabilities often to different roles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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