• Resolved Mark Wilkinson

    (@wpmarkuk)


    I thought this would be easy and actually maybe it is, but after lots of searching it does seem rather complicated and difficult to find help and a solution. Here goes…

    I have a site that am using a custom post type on. As normal I added the site without setting any capabilities etc. I have Authors on the site that I want to be able to edit their own pages and posts, which I have succeeded with. However I only want specific users to be able to do anything with my custom post type (see, edit, add and delete).

    Therefore I thought a relatively good solution would be to make it so that only users with a certain capability get to see the custom post type. If they don’t have that capability they don’t even see the menu item in the WordPress admin. I could then assign users this capability as and when need with the User Role Editor plugin.

    How would be best to go about this? Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    When you register your CPT, it’s possible to set up various capability schemes just for that CPT. You can then assign CPT capabilities to individual users, an existing role, or a newly created role. Adding roles and adding capabilities to those roles isn’t that well documented in the Codex. It’s possible to figure out what’s needed by studying the class definitions in wp-includes/capabilities.php, it’s not that complicated.

    If you study the source code, you’ll see the properties are stored in the options table, so it’s not necessary to run the code every time WP loads. I am a big proponent of developing my own custom code and avoid installing a slew of plugins to meet my needs. Yet I have found it convenient to use one of the roles and capabilities plugins to easily manipulate these properties. I use Members, I like it, it works fine, but that is not to diminish any of the others available.

    Thread Starter Mark Wilkinson

    (@wpmarkuk)

    Thanks for coming back to me with that and I will take another look at the resource you have outlined. That was in fact the place I started however I could not quite get my head around it. Will take another look.

    Thanks for your help.

    Thread Starter Mark Wilkinson

    (@wpmarkuk)

    Found the answer eventually thanks for looking at the way other plugins have done this, most notably the Whistles plugin by Justin Tadlock. Below is my solution.

    When register the post type with register_post_type you can pass capability types like this (for a custom post type named bulletin):

    'capability_type' => 'bulletin',
    'map_meta_cap' => true,
    'capabilities' => array(
    
    // meta caps (don't assign these to roles)
    'edit_post'              => 'edit_bulletin',
    'read_post'              => 'read_bulletin',
    'delete_post'            => 'delete_bulletin',
    
    // primitive/meta caps
    'create_posts'           => 'create_bulletins',
    
    // primitive caps used outside of map_meta_cap()
    'edit_posts'             => 'edit_bulletins',
    'edit_others_posts'      => 'manage_bulletins',
    'publish_posts'          => 'manage_bulletins',
    'read_private_posts'     => 'read',
    
    // primitive caps used inside of map_meta_cap()
    'read'                   => 'read',
    'delete_posts'           => 'manage_bulletins',
    'delete_private_posts'   => 'manage_bulletins',
    'delete_published_posts' => 'manage_bulletins',
    'delete_others_posts'    => 'manage_bulletins',
    'edit_private_posts'     => 'edit_bulletins',
    'edit_published_posts'   => 'edit_bulletins'
    ),

    Then you need to assign the these capabilities to users on plugin activation (or another appropriate hook) to allow them to use the post type:

    function mdw_plugin_activation() {
    
    	/* Get the administrator role. */
    	$mdw_role = get_role( 'administrator' );
    
    	/* If the administrator role exists, add required capabilities for the plugin. */
    	if ( !empty( $rhsstf_role ) ) {
    
    		$mdw_role->add_cap( 'manage_bulletins' );
    		$mdw_role->add_cap( 'create_bulletins' );
    		$mdw_role->add_cap( 'edit_bulletins'   );
    	}
    
    }
    
    register_activation_hook( __FILE__, 'mdw_plugin_activation' );

    i just want to say thank you for this post. It helped a lot.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Capability for Custom Post Type’ is closed to new replies.