Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Can't add new capabilities with a - in the name #80

Closed
fprezat opened this issue Jan 5, 2016 · 6 comments
Closed

Can't add new capabilities with a - in the name #80

fprezat opened this issue Jan 5, 2016 · 6 comments

Comments

@fprezat
Copy link

fprezat commented Jan 5, 2016

I try to add a new custom capability from the wordpress admin.
This capability is linked to a custom post type named page-arrivee.

The isssue is :
When i try to add the capability read_page-arrivee to a specific user role, it adds in fact the capability read_pagearrivee removing the "-".

Is it possible to avoid this ?

Thank you.

@justintadlock
Copy link
Owner

Capabilities really shouldn't have hyphens in them. Is there a particular plugin that you're using that's creating this capability? Maybe we can get in touch with the author to have them switch to an underscore.

@fprezat
Copy link
Author

fprezat commented Jan 6, 2016

I can't change the hyphen in the name, because the custom post type i have registered contain the hyphen in the name. And i have numerous posts with this custom post type, so if i change the name i lose every post created with this particular custom post type...

@justintadlock
Copy link
Owner

The name of your post type is irrelevant. If you're registering a post type, you have complete control over the names of the capabilities. Example: https://gist.github.com/justintadlock/6552000#file-register-post-type-php-L93

As a sidenote: it's non-standard to hyphenate post type names as well. It creates weird hook names sometimes with both hyphens and underscores.

@julien-hedoux
Copy link

Hi, What do you sugguest when you have a complex custom post type name? (about syntax).
I understand then map_meta_cap feature create dynamical capabilities but in this way, hyphens and underscores are bad. Calling a long string without seperator isn't a good way too :(

But could you explain to me the diff between a primary cap and a map_meta_cap ?
I see your comment in your gist but still not really understood all part.

@justintadlock
Copy link
Owner

This isn't really the place for me to teach how to build CPTs, so I'm going to keep this short and simple. In general, the naming scheme should follow these rules:

  • All lowercase letters (e.g., post instead of Post).
  • Use underscores to separate multiple words (e.g., nav_menu_item).
  • Use the singular rather than plural (e.g., page instead of pages).

As for caps, you should be setting the entire capabilities array when registering your post type. All of these lines: https://gist.github.com/justintadlock/6552000#file-register-post-type-php-L93-L117

You can name your capabilities anything. That's up to you. They don't have to match your post type name at all (usually, they should be similar though just for organizational purposes).

If you want an example of a real-world plugin, here's one I built: https://github.com/justintadlock/custom-content-portfolio/blob/master/inc/functions-post-types.php

@julien-hedoux
Copy link

ok thanks. Yes we know that's not the good place but the background aim is why with your plugin it's forbidden. Understood now. The last question is the difference in caps between a cap with and without ending "s".
Ex : edit_my_cap and edit_my_cpt_s.
That's what the real question about my comment. not really how to create the CPT ;)
You are right about the naming scheme

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants