Creating custom user roles

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.

1 Answer
1

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

Leave a Comment