• 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 - 16 through 30 (of 46 total)
  • Ok, well that’s strange, it works for me, using your code, and with the capability uncommented.

    // I didn't use your function or hook, i have my own so i just copied the register_post_type code for testing.
    
    	register_post_type( 'super_duper', array(
    		'labels' => array(
    			'name' => __( 'Super Dupers' ),
    			'singular_name' => __( 'Super Duper' )
    		),
    		'public' => true,
    		'capability_type' => 'super_duper'
    	) );

    I did notice however that the Members plugin fails to detect the new capabilities registered by the custom post type, i had to add a couple for testing, namingly edit_super_dupers and publish_super_dupers (two of the capabilities available when you register the custom post type – listed under capabilities on the register_post_type codex page).

    I updated the editor role(testing), by default an editor can edit the custom type already, so i unchecked the two capabilities i had added in (edit_super_dupers and publish_super_dupers). The result was as expected, the editor could no longer edit or add posts of a type super duper.

    I corrected the stray comma in your code above (you may have noticed, however, the code worked with that error still present, so i don’t believe it’s the cause of your issue).

    Perhaps something was fixed in the latest code, i’ll go back and test 3.0 if you want (3.1 alpha on my install).

    Would you like me to test a particular role maybe, add a new role? If you want to give me a specific usage to test for i’ll be around for another hour or so, possibly two.

    NOTE: When i register post types and taxonomies i use a hook like so..

    add_action( 'init', 'register_taxonomies_and_types', 0 );

    0 gives the hook highest priority, this might make a difference, it may not, just thought i’d mention it as it’s the one thing different on my end (apart from the WP version).

    I’ll install 3.0 later today (maybe tomorrow – it’s the weekend have no idea what my plans are yet), and see if i can make the same progress, thus far, all seems to work as expected on my end.

    Thread Starter richarduk

    (@richarduk)

    Cool, thanks, I’ll double-check what I’m doing. Bear with me whilst I work it all out, if you’re not around for a while I’ll post what result I get anyhow.

    Strange, I seem to be the only person on the planet experiencing this, I’ll strip my functions.php right down and see if it was anything to do with that.

    Thanks again.

    Thread Starter richarduk

    (@richarduk)

    Right …

    First results.

    I stripped down functions.php just in case, added the code, no change – can’t get super_duper buttons to show.

    Then I added the capabilities as you suggested above, using the plugin, and it worked fine. I basically followed all your steps, and no problems. I also used my code, and I used your code, and there were no problems. Exactly as you noted above.

    So .. the members plugin is a lovely little plugin, and is obviously doing the trick, but somehow the only way to add super_duper capabilities is through the plugin. It doesn’t seem to be possible to add super_duper capabilities using code alone. I’m sure it is possible, but I don’t know how to do it ??

    You can define the capabilities yourself (what they’re called) by registering them inside register_post_type, ie. the capabilities arg..

    They still exist without the members plugin (default names), you just won’t see them in the members plugin until creating them there, or at least that seems to be the case, the code worked before i even started with the members plugin (ie. i could see the super duper posts link in the admin menu and create super duper posts before doing anything with the plugin).

    Thread Starter richarduk

    (@richarduk)

    How strange. I’m glad that it’s working, it’s obvious that I’m doing something wrong.

    Is it possible that you can go back to version 3 and test it?

    I’ve uploaded a functions.php file with literally nothing but your code in it and when it’s commented out the Super_Duper button shows, but when it’s uncommented it doesn’t show.

    Maybe this is a bug that’s now been ironed out?

    Perhaps, it’s certainly possible, that’s why i mentioned i’m using 3.1 (it could be all the difference).

    I’ll install 3.0 when i’m back at the main PC and have some time to spare. Will let you know how i get on. I’m just trying to avoid using that PC at the moment, it’s in my bedroom, which in the current UK weather becomes very hot (small room, PC on = very hot very quickly), managed a few hours earlier, but it’s just unbearable with the hot weather we’re getting right now (even with a fan blowing toward me and the windows open).

    I’ll let you know how i get on once it’s installed and tested.. ??

    I just went through the steps on 3.0, I can confirm the behaviour with @richarduk

    comment out the custom role, the super_duper appears in admin
    uncomment, super_duper is missing

    Good to know, can you guys confirm if behaviour is the same using a different capability type, such as post?

    Capability type post works properly for me….

    Thread Starter richarduk

    (@richarduk)

    Ditto. super_duper capability type doesn’t work, post capability type does work.

    ??

    Certainly can confirm the problem, even adding the capabilities to the roles manually doesn’t help.

    Seems to be fixed for 3.1 though, so i guess that’s good news.

    Thread Starter richarduk

    (@richarduk)

    I’ve manually updated to the latest nightly build – about twenty minutes ago – and the problem is still there.

    I would be interested to know if anyone else can confirm this, as that would make it an unsolved bug.

    The reason I’m interested is that I want to get a custom post type with its own unique capabilites to be assigned to a unique role e.g. the role ‘Designer’ is only capable of editting ‘Graphics’ posts and not ordinary posts or Pages. I haven’t been able to do this yet.

    Not sure why the code functioned differently first time but i’ve just wiped the 3.1 install and started again, seems this problem is actually present for 3.1 to.. (my mistake, sorry).

    Although i’m pretty sure creating a role, some caps, and a post type specifically for that role should be simple enough.. just need to test some code (for your ref: wp-includes/capabilities.php – is the file that deals with roles and caps).

    I’d suggest using the members plugin personally, as a matter of convenience. Justin Tadblock is generally very active in the community, so if it’s a matter of worry about the author not being around, i personally don’t think Justin is likely to vanish without warning.

    I’m going to run some simple role/cap code for testing in any case, so i’ll report back the procedure(should make for a good eye opener on how to use the code – assuming i can wrap my head around it)… ??

    EDIT: This is a good example of registering roles and caps, will save me needing to write one (it covers the necessary parts).
    https://sillybean.net/wordpress/users-and-roles/creating-roles/

    Thread Starter richarduk

    (@richarduk)

    Look forward to it ??

    I took the disappearance of the buttons as symptomatic of deeper problems, since I couldn’t get any of it to work. I’d be greatly interested in some code that does work.

    I take on board what you’re saying about the plugin author being a stalwart of the WP community, but I’m authoring a theme and although I gladly reference help, tutorials and plugins (and his is excellent) I’m trying by default to have code do as much as possible wherever possible.

    See the example on the link i posted previously, it’s about as simple and easy as an example could be.

    NOTE: When you add a role, once it’s registered that’s it, you don’t need to keep running the add role code, once you’ve created the role it’s there until you remove it. I think the same may be true for capabilities to, you register them once, then it’s done (so no need to always register them on init like you would for post types or taxonomies).

    I made my own page for showing roles and caps (purely for visibility over the roles and caps), take a look, i can provide the code (somewhat quick and hacky – ie. i’ve just thrown it together), but it’s useful all the same.

Viewing 15 replies - 16 through 30 (of 46 total)
  • The topic ‘Anyone managed to get custom post types capabilities working?’ is closed to new replies.