Forums

Anyone managed to get custom post types + capabilities working? (47 posts)

  1. richarduk
    Member
    Posted 1 year ago #

    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

    http://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',
    
    		)
    	);
    }
  2. greatdanemaniac
    Member
    Posted 1 year ago #

    I think that capability type could only be "post" or "page", but I'm not sure. I hope you figure it out.

  3. Mark / t31os
    Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/register_post_type
    http://codex.wordpress.org/Function_Reference/register_post_type#Arguments

    capability_type
    (string) (optional) The post type to use for checking read, edit, and delete capabilities.
    Default: "post"

    I imagine that you cannot use the post type you're registering as the post type with which to look at for capabilities (as you're just registering it at that point), try post, page or another post type.

    I agree with greatdanemaniac.

  4. richarduk
    Member
    Posted 1 year ago #

    Thanks for your replies.

    I have snippets of code from all over the internet but I've still not managed to make it work.

    The whole point of the custom post type capabilities is that we can change them. After that, it should be possible to assign them to a new role.

    This would mean that we could end up with a role of 'Designer' who can only edit 'Graphics' posts and not ordinary posts or Pages, say.

    I've been going round in circles. Am I doing something wrong, or is just that few people are trying to do this yet?

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    Of course, capability control means nothing without being able to choose who has the capabilities. That’s what my Members plugin was created for. It’s ideal for this scenario.

    that's from teh tutorial you linked. I thhink you need to set up the role super_duper in WP, otherwise, you are making the capabilities for the post super_duper, but you have no super_duper, so nobody is allowed to edit the post. You have to choose who has the capabilities to edit the new post type..... (I think....I'm just kind of guessing after reading the tutorial)

  6. Mark / t31os
    Moderator
    Posted 1 year ago #

    That relates to my next question.

    How are you registering the role and/or capabilities and can i see the code you're using to do so?

    NOTE: Maybe you might consder using a plugin to register the roles/caps just to see if behaviour works any differently.

    http://wordpress.org/extend/plugins/members/ - would be my suggestion, best role plugin i've used to date.

  7. richarduk
    Member
    Posted 1 year ago #

    Oh yes yes yes.
    Many times, in many ways.

    Sorry to sound frustrated.

    Here's a link to related? problems? http://core.trac.wordpress.org/ticket/14122

    Here's my code. It's a complete jumble, because I try things out, comment them out when they don't work, borrow bits that I think might work, start a new page with the bits I do know work etc.

    So this is a conglomeration of three pages of code. More of a pointer to some of the things I've been trying.

    *code moderated*
    Replaced with pastebin.
    http://wordpress.pastebin.com/ftH7RjMm

  8. richarduk
    Member
    Posted 1 year ago #

    Sorry, thought I'd lost my post

  9. Mark / t31os
    Moderator
    Posted 1 year ago #

    Sorry had to moderate your code(another mod already removed the post), please don't post huge portions of code (lots of scrolling for those of us on small screens - we usually moderate any code over 15-20 lines).

    Back to the topic at hand though, have you seen my suggestion above regarding using a role plugin?

  10. richarduk
    Member
    Posted 1 year ago #

    Hmmm. I have an inherent aversion to plugins when code will do the trick, because with a plugin I'm dependent on the plugin author for the functioning of my site. So plugins for me are only for non-critical functions.

    That said, the developer at the site I linked to in my first post has a role plugin. I've already tried to figure it out once, but I can have another look and see if there's anything obvious that I'm missing.

  11. Mark / t31os
    Moderator
    Posted 1 year ago #

    Yes, Members is Justin's plugin, same guy.. ;)

    I can install it if you have problems understanding how to use it..

  12. richarduk
    Member
    Posted 1 year ago #

    Thanks:)

    I meant figuring out the code.

  13. Mark / t31os
    Moderator
    Posted 1 year ago #

    If you can clarify what you need help understanding i'll do what i can to help.. :)

    Plugin suggestion was simply to take the cap/role creation out of the equation, i've used the plugin, i know it works, and that's one thing we could eliminate as a possible cause (almost, can't ever be 100% sure), but if it doesn't make a lick of difference to your code not working then obviously we can drop that idea... and move onto looking back at the register_post_type code.

    Just trying to narrow down exactly which area is the problem, i currently suspect it's with the role/cap creation as RVoodoo suggested above also. I could be wrong, and if i am i'll go back over the other code with you, but for now i'd ask that you try the plugin route to save me having to test code that i'm not familiar with, although i'm happy to do that if the need arises (time permitting).

  14. richarduk
    Member
    Posted 1 year ago #

    Thanks, t31os_, that's a really generous offer. I'm going to start with a clean site, instal the plugin, then report back :-)

  15. richarduk
    Member
    Posted 1 year ago #

    Ok ...

    Completely clean instal (well, oldish site, but I haven't messed around with roles and capabilities on it.) Minimal plugins.

    Installed the 'Members' plugin noted above. Acitvated Edit roles, new roles, content permissions.

    Made sure Administrator had every single role possible, including edit_roles.

    Everything fine.

    Added this to functions.php (bare bones code)

    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',
    
    		)
    	);
    }

    'Super Duper' post type appeared in Admin sidebar.

    Uncommented 'capability_type' => 'super_duper', from functions.php and 'Super Duper' disappeared from sidebar.

    Haven't proceeded any further i.e. adding a new role in functions.php or from the Admin screen.

  16. Mark / t31os
    Moderator
    Posted 1 year ago #

    I'll recreate your steps on my install and let you know how i get on, bear with me a little while, i'm just making a few quick responses to other threads then i'll take a crack it.

  17. Mark / t31os
    Moderator
    Posted 1 year ago #

    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.

  18. richarduk
    Member
    Posted 1 year ago #

    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.

  19. richarduk
    Member
    Posted 1 year ago #

    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 :-)

  20. Mark / t31os
    Moderator
    Posted 1 year ago #

    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).

  21. richarduk
    Member
    Posted 1 year ago #

    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?

  22. Mark / t31os
    Moderator
    Posted 1 year ago #

    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.. ;)

  23. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    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

  24. Mark / t31os
    Moderator
    Posted 1 year ago #

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

  25. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    Capability type post works properly for me....

  26. richarduk
    Member
    Posted 1 year ago #

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

    :-)

  27. Mark / t31os
    Moderator
    Posted 1 year ago #

    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.

  28. richarduk
    Member
    Posted 1 year ago #

    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.

  29. Mark / t31os
    Moderator
    Posted 1 year ago #

    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).
    http://sillybean.net/wordpress/users-and-roles/creating-roles/

  30. richarduk
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic