Register Post Types, Using Capabilities:

Posted: February 25, 2011 in Knowledge Base

Register Post Types, Using Capabilities:

When using custom post types in wordpress, I did some research and found some permission levels. These things are not exclusively explained in WordPress documentation (http://codex.wordpress.org/Function_Reference/register_post_type ).

For example, I am adding a custom post type called book, then I would add following code to my plugin:

add_action('init', 'add_books_init');
function add_books_init()
{
  $labels = array(
    'name' => _x('Books', 'post type general name'),
    'singular_name' => _x('Book', 'post type singular name'),
    'add_new' => _x('Add New', 'book'),
    'add_new_item' => __('Add New Book'),
    'edit_item' => __('Edit Book'),
    'new_item' => __('New Book'),
    'view_item' => __('View Book'),
    'search_items' => __('Search Books'),
    'not_found' =>  __('No books found'),
    'not_found_in_trash' => __('No books found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Books'

  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array('slug'=>'mybooks'),
    'capability_type' => array('book','books'),
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array('title', 'comments', 'editor'),
	'taxonomies'=>array('book'),
	'exclude_from_search'=>false
  );
  register_post_type('book',$args);
}

Notice the argument capability_type, which is optional in register post type. However, if we do not add this argument, the new book section will be registered with permissions as that of posts.

Now, in our case, the books sections is registered as a new entity, having its own permission set. We need to assign some new permissions to any user to access this entity (to an administrator also!)

In our case, the permissions that need to be assigned are mentioned below:

edit_books
edit_book
publish_books
publish_book
delete_book
delete_books
edit_others_posts
read_private_books

We can use some sort of plugins or use wp_capability variable to assign these permissions. I used capsman plugin for the same. Have a look at the screenshot:

Comments
  1. Gideon says:

    thanks that clarifies some
    but it is still a mystery to me why WP makes these things so difficult to access..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s