Creating custom user roles

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