Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Adding a role is very simple. Creating custom capabilities is a little bit more to wrap your head around. When you register your custom post type, you define your capabilities for it. Essentially it is an array of "what do you want to count as this?". My example below will clarify this statement.

$caps = array(
    'edit_post' => 'edit_cpt_name',
    'edit_posts' => 'edit_cpt_names',
    'manage_posts' => 'manage_cpt_names',
    'publish_posts' => 'publish_cpt_names',
    'edit_others_posts' => 'edit_others_cpt_names',
    'delete_posts' => 'delete_cpt_names'
);

So, you would obviously replace "cpt_name" with your custom post type's slug (or anything you want really). The items on the left are the default capability names (there are more, so refer the the register_post_type entry in the codex). Whatever the capabilities you declare in your custom post type registration, you need to also give the User Role those capabilities:

add_role('basic_contributor', 'Basic Contributor', array(
    'read' => true,
    'edit_posts' => false,
    'edit_cpt_name' => true, // True allows that capability
    'edit_cpt_names' => true,
    'publish_cpt_names' => true,
    'edit_others_cpt_names' => false, // Use false to explicitly deny
    'delete_cpt_names' => false
));
share|improve this answer
    
Thank you. I'll be trying it when I reach that part of the project. –  RRikesh Aug 23 '12 at 9:48
    
I updated my question. Thanks for the help –  RRikesh Aug 23 '12 at 13:29
    
@rrikesh I think that's just about as good as it gets, unless you write a function which accepts an array of "roles", and then also an array of "capabilities" –  Eric Holmes Aug 28 '12 at 20:33
    
Something like add_caps_to_roles( {'administrator', 'editor'}, {'delete_cpt', 'manage_cpt'}); –  Eric Holmes Aug 28 '12 at 20:34
    
Thanks a lot Eric! –  RRikesh Aug 29 '12 at 5:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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