• I’ve found a few examples of how to create custom post types but I’ve not managed to get them to work e.g from here

    https://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

    The problem is capability_type. If I comment it out everything works; if I leave it there the labels disappear.

    What on earth am I doing wrong? I’ve stripped the code down to the bare minimum.

    add_action( 'init', 'create_my_post_types' );
    
    function create_my_post_types() {
    	register_post_type( 'super_duper',
    		array(
    			'labels' => array(
    				'name' => __( 'Super Dupers' ),
    				'singular_name' => __( 'Super Duper' )
    			),
    			'public' => true,
    	'capability_type' => 'super_duper',
    
    		)
    	);
    }
Viewing 15 replies - 31 through 45 (of 46 total)
  • Thread Starter richarduk

    (@richarduk)

    Could you post the code, please? I’d be very grateful.

    Just needed to add custom role and capability support and test some code further.

    It seems there are some inconsistencies with the necessary caps required for a role to edit, post or manage custom post types, once a role has the required caps however it does allow a user with that role to manage that content type.

    I’ve been testing the code in 3.0, so it’s most certainly working and will work for you.

    All my test code is inside the function for listing roles and caps, so you’ll just have to dig in and play around. 'capability_type' => 'super_duper' works when the necessary caps are assigned to a given role… if it’s not in the menu, it’s because your user’s role doesn’t have that capability – administrators won’t necessarily have the capabilities to manage that post type unless you give that role the capabilities (i think that was the initial issue with the disappearing menu item – solved when adding the caps via the members plugin previously, for 3.0 and 3.1).

    Now, onto the inconsistencies i spoke about earlier. When registering a custom post type, the register_post_type codex entry says the following in reference to post type capabilities..

    • ‘edit_’ . $capability_type (edit_post) – The meta capability that controls editing a particular object of this post type. Defaults to “edit_ . $capability_type”
    • ‘edit_’ . $capability_type . ‘s’ (edit_posts) – The capability that controls editing objects of this post type as a class. Defaults to “edit_ . $capability_type . s”
    • ‘edit_others_’ . $capability_type . ‘s’ (edit_others_posts) – The capability that controls editing objects of this post type that are owned by other users. Defaults to “edit_others_ . $capability_type . s”
    • ‘publish_’ . $capability_type . ‘s’ (publish_posts) – The capability that controls publishing objects of this post type. Defaults to “publish_ . $capability_type . s”
    • ‘read_’ . $capability_type (read_post) – The meta capability that controls reading a particular object of this post type. Defaults to “read_ . $capability_type”
    • ‘read_private_’ . $capability_type . ‘s’ (read_private_posts) – The capability that controls reading private posts. Defaults to “read_private . $capability_type . s”
    • ‘delete_’ . $capability_type (delete_post) – The meta capability that controls deleting a particular object of this post type. Defaults to “delete_ . $capability_type”

    Notice the use of plurals for some capabilities and not for others. As far as i can tell the actual required capability varies and some require plural and others not so, and to be quite frank i didn’t have time to sit and work out which ones are correct and which are not, so i just added all caps plural and non-plural (you’ll see what i mean when you look at the code below).

    Sample code.
    https://wordpress.pastebin.com/HeeHJcLz

    Small note: When adding or removing a role, if you’re viewing the roles and caps page you’ll need to refresh once or twice before the role will appear or disappear from the page (just due to placement of the add/remove role code – it’s just there out of convenience).

    The tester role in the sample code will be the only role that can see, edit and publish super_duper post types if you now switch your register post type capability_type arg to super_duper. If you want to see the post_type whilst logged in as admin, you’ll need to give the administrator role the capabilities to manage the post_type, like in the sample code for the tester role.

    Hope that helps, and i didn’t waffle on too much…. ??

    Made a small development regarding the correct caps(plurals vs singular) for a post type.

    get_post_type_object will retrieve an object with all the info about a post type, including the required caps for managing the post type.

    I’ll refine the code after some sleep, but in the mean time it should give you something to toy with.. ??

    Continuing from the last post, the following caps are what’s required for the role to manage that post type (assuming you’ve just used default, as we’ve been doing).

    'read' => 1,
    'edit_super_duper' => 1,
    'edit_super_dupers' => 1,
    'edit_others_super_dupers' => 1,
    'publish_super_dupers' => 1,
    'read_super_duper' => 1,
    'read_private_super_dupers' => 1,
    'delete_super_duper' => 1

    You can see the role added with only the necessary caps in the following screenshot. Note, the admin doesn’t have the caps, so it can’t see the menu item.
    https://t31os.files.wordpress.com/2010/07/rac-01.png

    Now look at the following screenshot from IE where i’m logged in with a user that has the tester role, notice the menu items are now present (the role can add, edit, and so forth, and was tested to make sure the capability did actually work).
    https://t31os.files.wordpress.com/2010/07/rac-02.png

    I’ll give you a refined version of my code if you like, but all i’ve changed is the caps i’m registering in the role (listed above).

    If you’d like an example of removing or adding caps to existing roles feel free to ask.. ??

    Thread Starter richarduk

    (@richarduk)

    ??

    I certainly shall, though I think I’ve used up all my credit / positive karma ??

    Anyhow, I’ll play around with that tomorrow, it looks like there’s a lot there to think about.

    Well, this is certainly a long thread. I’ll try to keep it simple and help you on your way.

    When you register a custom post type, you’re not registering new capabilities, so the Members plugin wouldn’t recognize them as so. Capabilities have to be attached to a role/user for them to “exist,” so to speak.

    You won’t have access to things with custom caps unless you literally assign these caps through the role editing screen (Members plugin). That means typing them rather than checking the boxes.

    If you use post for the capability_type, all is right with the world because you’re just using something that’s already set up. But, if you’re using something custom…well…it’s custom. You’ll have to do a little legwork.

    Meta capabilities (the equivalent of edit_post, delete_post, and read_post) aren’t mapped for a custom capability type. You have to do the mapping for these yourself. See this post for more details on how to do this:
    https://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types

    Well your blog post certainly clears up some things, i’d actually read it before to (didn’t come up on google when searching for role related info recently though – my choice of key words wouldn’t have matched, my loss). Basically covers what i’ve come to realise and was getting at throughout the thread (but better and more in-depth then my ramblings, hehe).

    NOTE: Some of the caps you have in your example code are also registered by default when the post type is registered without setting the caps in the args being passed in (some seem to get missed, for whatever reason).

    eg. (if you were to take Richards post type above)

    register_post_type( 'super_duper', array(
    	'labels' => array(
    		'name' => __( 'Super Dupers' ),
    		'singular_name' => __( 'Super Duper' )
    	),
    	'public' => true,
    	'capability_type' => 'super_duper',
    	'show_ui' => true
    ) );

    This is what you’ll see in the caps array for that post type’s object.

    'publish_posts' => 'publish_super_dupers',
    'edit_posts' => 'edit_super_dupers',
    'edit_others_posts' => 'edit_others_super_dupers',
    'read_private_posts' => 'read_private_super_dupers',
    'edit_post' => 'edit_super_duper',
    'delete_post' => 'delete_super_duper',
    'read_post' => 'read_super_duper',

    However, as you said above (and i’d come to realise whilst getting to understand the code), a role isn’t given a custom capability by default (why i wrote my hacky role/cap page – visibility), any non-default/custom cap must be assigned to a role, be it an existing role, or a newly added role.

    ??

    Thread Starter richarduk

    (@richarduk)

    Thanks!

    I haven’t forgotten this thread, I’ve just got other things on my plate at the moment.

    Thank you again!

    You’re welcome, happy to help… and was an eye opener for me.. ??

    Hello,

    Pardon my ignorance, but I’m trying to get these custom caps setup, and if I have deduced this thread correctly, the process is this

    (Im using members plugin)

    1. Add custom caps to whatever role you desire. For me, my custom cap is ‘news’ (capability_type => ‘news’ .. when I register the custom post type)
    2. So I typed in and added, edit_news, publish_news, etc etc.. there’s quite a few.
    3. Refresh
    4. Add capability type of ‘news’ to custom post type
    4. ???
    5. It works?? But it doesn’t. ??

    Hi,

    Print out the post type’s caps array and make sure the caps shown match up with those you have assigned to your custom role.

    global $wp_post_types; // This line should only be needed if printing from within a function
    print '<pre>';
    print_r( $wp_post_types['news']->caps );
    print '</pre>';

    And you should see an array of caps, like i’ve shown in one of my previous replies.

    I cant get that code to print anything, anywhere. Tried it with other types too.

    So just print out all the post types?

    global $wp_post_types;
    print '<pre>';print_r( $wp_post_types );print '</pre>';

    Failing that get the post type object..

    print '<pre>';print_r( get_post_type_object('cpt') );print '</pre>';

    Where cpt is your custom post type name.

    If neither work, then my next question is, are you using WP 3.0?

    Ah, got it. The second function worked. Because my category is already plural, it added another s, so the caps are edit_newss .. etc. Cheers!

    Thread Starter richarduk

    (@richarduk)

    For anyone who wants to find out more about the post types on their site and the potential capabilities assigned to those post types, and the roles on their site and the capabilities assigned to those roles:

    <?php
    print '<pre>';
    print_r( get_post_type_object('review') );
    print '</pre>';
    //Tons of information about the   review    post type (change review to your post type)
    //Paste anywhere on a theme template file outside the loop.
    ?>
    
    <?php
    global $wp_post_types;
    print '<pre>';print_r( $wp_post_types );print '</pre>';
    // Tons of information about all the different post types on the site
    //Paste anywhere on a theme template file outside the loop.
    ?>
    
    <?php  echo '<br /><br /><h3>Roles</h3>';
    	foreach ( $wp_roles->role_names as $role => $name ) :
    		echo '<br /> <br />';
    		echo '<pre> Role displayed in Admin as ' . $name ;
    		echo  '     Database entry: '  . $role . '</pre>';
    
    		echo '<h5> Capabilities assigned to the role of ' . $name. '</h5>';
    		// print_r( $caps);
    		echo '<pre>';
    		$rolename = get_role($role);
    		$caps = $rolename->capabilities;
    			foreach ($caps as $capability => $value):
    				echo  $capability . '?'.  $value . "\n" ;
    			endforeach;
    		echo '</pre>';
    	endforeach;
    
    //Show the role display name and the name used in the database for each role on the site.
    //Show the capabilities assigned to that role
    //Paste anywhere on a theme template file outside the loop.
    //Thanks to Greenshady: https://www.ads-software.com/support/topic/get-a-users-role-by-user-id
    // Thanks to:  https://sillybean.net/wordpress/creating-roles/
    ?>
Viewing 15 replies - 31 through 45 (of 46 total)
  • The topic ‘Anyone managed to get custom post types capabilities working?’ is closed to new replies.