[wp-hackers] Custom Post Type + Capabilities & Roles

Jake Goldman wphackers at jakemgold.myfastmail.com
Sun Jun 27 22:45:30 UTC 2010


  This isn't right, Chocks.

And I'm now virtually certain - after trying a few other things - that 
granular / "special" capabilities on custom post types is broken in 3.0.

As to your code, Chocks, you can submit your posts for review. It still 
shows the "not allowed" error in the editor, though it does allow 
submission anyhow. Problem is every other "contributor" can edit your 
posts too. I think it's getting confused by the existence of the meta 
capability in the roles primitive capabilities.

I'm 99% certain WordPress should handle the meta capabilities (the 
"singulars") on its own (we should not have to assign those).

Here's some pretty basic code (from my functions file) that registers a 
new custom post type with a special (non-default) 'capability_type'. It 
is followed by some code that adds a new role with the plural 'edit_' 
capability (they still get errors trying to edit content or submit for 
review). That latter block of code also and adds every other custom 
plural capability to roles with similar (generic) post roles, yet 
administrators cannot edit others posts!

I can confirm that this code gives the "guestblogger" role 
"edit_guest_posts" capability, and that the 'guest-blogs'  content type 
has 'edit_guest_posts' as the value of its 'edit_posts' capability.

Andrew - I'd really like your opinion on whether there's something wrong 
with this code, and if not, whether the roles work as expected for you.


register_post_type('guest-blogs',array(
     'label' => 'Guest Posts',
     'singular_label' => 'Guest Blog Post',
     'description' => 'Guest blog posts from the members.',
     'public' => true,
     'capability_type' => 'guest_post',
     'supports' => 
array('title','editor','comments','revisions','author','excerpt')
));

// remove_role('guestblogger');

if ( !get_role('guestblogger') )
{
     global $wp_roles;
     if ( !isset( $wp_roles ) ) $wp_roles = new WP_Roles();

     $caps = $wp_roles->get_role('subscriber')->capabilities; //let's 
use the subscriber as the base  capabilities
     $wp_roles->add_role( 'guestblogger', 'Guest Blogger', $caps );
     $wp_roles->add_cap( 'guestblogger', 'edit_guest_posts' );

     // add guest blogger capability to others
     foreach ( $wp_roles->role_objects as $role ) :

         if ( in_array( 'edit_posts', $role->capabilities ) ) 
$role->add_cap( 'edit_guest_posts' );
         if ( in_array( 'publish_posts', $role->capabilities ) ) 
$role->add_cap( 'publish_guest_posts' );
         if ( in_array( 'edit_others_posts', $role->capabilities ) ) 
$role->add_cap( 'edit_others_guest_posts' );
         if ( in_array( 'read_private_posts', $role->capabilities ) ) 
$role->add_cap( 'read_private_guest_posts' );

     endforeach;
}


Jake


On 6/27/2010 4:31 PM, Joaquin Rodriguez Montero wrote:
> Jake,
>
> Based on your code I added:
> 'capabilities' =>  array( 'edit_post' =>  'guest_question','edit_posts' =>
> 'guest_questions'),
> And that allowed me to submit it for review.
>
> I couldn't succeed to show the new post type on any other user than the
> guest blogger.. does this bit helps what are you trying to achieve?
>
> Chocks


More information about the wp-hackers mailing list