Capabilities set for Custom Post Type

Special capabilities for custom post type

custom post type capabilities

You configured custom post type at your WordPress managed site. You did it with special plugin or manually. It works good. But what to do, if it uses standard WordPress posts permissions, e.g. ‘edit_posts’, but you need to separate access for standard and custom posts for authors and editors? Does WordPress offer a decision?
Yes. Custom post type may have custom capabilities set defined and it could be fully different from the standard post capabilities set.
What variants do you have?
1st, – use plugin which can manage not just custom post types, but custom capabilities set for those custom post types too. Good example is Custom Post Types UI WordPress plugin. I used it myself long time at my personal Russian language site FiNovel.com. Inspite of little quant of “Works” reviews at WordPress plugins repository page, I confirm – it works.

I checked that with WordPress 3.3.2 and with latest release of upcoming new WordPress 3.4. It’s a puzzle for me, why people downloads free software more than 100 times a day, but do not send a little appreciation to the plugin author, just clicking “Works” button at WordPress.org plugin’s page?

Custom post type UI plugin compatibility with WordPress

Compatibility

This way, free product, with more than 130,000 downloads in total, has so weak estimation.
If you use WordPress plugin, if it works for you well, if you like it, then spend a minute, register at WordPress, or sign-in, go to WordPress plugin page and click “Works” button there. As the author of 5 WordPress plugins, I very appreciate that myself :). Some times the lack of appreciation from users leads to the situation when really working popular plugin with hundreds downloads a day could have “Broken” mark at wordpress.org plugins repository, just for the lack of appreciation from successful installations owners and users.

Let’s return to the custom post type custom capabilities issue.
1st, you can force WordPress to use your own capabilities for custom post type, typing something different than default ‘post’ at “Capability Type” field of “Advanced Options” section at “Custom Post Types UI” plugin custom post type edit page, or somewhere else if you use other plugin.

Custom post type UI advanced options

Custom post type UI advanced options


2nd, you can find place in the source code of plugin or theme, which creates custom post type for your site, but with default ‘post’ capabilities set and modify it yourself. Where to find it? Seach source code for register_post_type function call. This function has 2 parameters: 1 – post type identifier, 2 – array of deep tuning post type parameters, which takes default values, if any is not sent apparently here. For full information look into wp-includes/post.php file and read comments from WordPress development team.

/**
 * Register a post type. Do not use before init.
 *
 * A function for creating or modifying a post type based on the
 * parameters given. The function will accept an array (second optional
 * parameter), along with a string for the post type name.
 *
 * Optional $args contents:
 *
 * ...
 * 
 * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
 *     * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
 *       capabilities, e.g. array('story', 'stories').
 * - capabilities - Array of capabilities for this post type.
 *     * By default the capability_type is used as a base to construct capabilities.
 *     * You can see accepted values in {@link get_post_type_capabilities()}.
 * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
 * 
 * ...
 *
 * @param string $post_type Post type key, must not exceed 20 characters
 * @param array|string $args See optional args description above.
 * @return object|WP_Error the registered post type object, or an error object
 */
 function register_post_type($post_type, $args = array()) {

Let’s suppose, you found register_post_type function call inside plugin source code, for example:

  $args = array (
                  'public' => true,
                  'labels' => array('name' => 'Cards', 'singular_name' => 'Card', 'add_new' => 'Add card'),
                  'show_in_menu' => true,
                  'menu_icon' => plugins_url('plugin_name/images/icon.png'),
                  'query_var' => true,
                  'rewrite' => true
                  );
 
   register_post_type('card', $args);

Access to the custom post type ‘card’ registered this way will be managed by standard WordPress capabilties set, based on ‘post’, that is ‘edit_posts’, ‘delete_posts’, etc. In order to make WordPress to use your own capability for this post type, like ‘edit_cards’, ‘delete_cards’, ‘publish_cards’, etc. you need to add 2 additional parameters to this function call arguments list: capability_type and map_meta_cap. Finally you should have:

  $args = array (
                  'public' => true,
                  'labels' => array('name' => 'Cards', 'singular_name' => 'Card', 'add_new' => 'Add card'),
                  'show_in_menu' => true,
                  'menu_icon' => plugins_url('plugin_name/images/icon.png'),
                  'query_var' => true,
                  'rewrite' => true,
                  'capability_type' => 'card',
                  'map_meta_cap' => true
                  );
 
   register_post_type('card', $args);

Making this changes you need to define your own custom post type capabilities set and include it to WordPress user roles according to your selection. You can make it with User Role Editor help. Capabilities set should include these capabilities:

  • edit_cards;
  • edit_others_cards;
  • publish_cards;
  • read_private_cards;
  • delete_cards;
  • delete_private_cards;
  • delete_published_cards;
  • delete_others_cards;
  • edit_private_cards;
  • edit_published_cards.

Final part. If you will use custom taxonomies (categories) for you custom post type, user without ‘edit_posts’ capability could not assign them to custom posts. So you need to overwrite default ‘edit_posts’ permission to the new one. Custom taxonomy is defined this way generally:

$args = array (
                  'hierarchical' => true,
                  'label' => 'Card Types',
                  'query_var' => true,
                  'rewrite' => true
                 );
 
   register_taxonomy('questions', NULL, $args);
   register_taxonomy_for_object_type('questions', 'question');

Add capabilities argument to the parameters list, e.g. it’s shown below:

$args = array (
                  'hierarchical' => true,
                  'label' => 'Card Types',
                  'query_var' => true,
                  'rewrite' => true,
                  'capabilities' => array('assign_terms'=>'edit_cards')
                 );
   register_taxonomy('questions', NULL, $args);
   register_taxonomy_for_object_type('questions', 'question');

Tags: , , ,