I created an interface that allows users to do a certain number of actions, each of them having a capability associated (including accessing the interface). I would like to have a base capability (primitive here?) that is required to have if a user wants to preform all of the other actions: if the user can delete an item, the user must also be able to access the interface.

Several plugins allow to add/remove capabilities: I would like to prevent the case when a user can delete but cannot access the interface from happening.

I understand that with post types and meta capabilities and map_meta_cap filter - I am having a hard time to apply that to my plugin, though.

I could check both all the time, but the permission hierarchy will eventually be more complex (say, 3 levels - user must have the capabilities access_interface, create_item and create_typeX_item to be able to create an item of type X) so it sounds silly to duplicate code all over the place instead of just checking one capability (in my example: create_typeX_item). I am NOT dealing with a custom post type, and it isn't an option, so I don't know if the use map_meta_cap filter is relevant in this case, and if so I don't know how to use it.

Any suggestions?

  • What exactly is the question? It looks like you have series of vague ideas about you want to do, and not really an addressable question at all.
    – s_ha_dum
    May 16, 2014 at 0:16
  • @s_ha_dum It isn't vague. I describe my case with as much details as I possibly can. If you want a summary (which kind of over-simplifies, but if it can help...): how do you make a capability dependent of another, given that these 2 capabilities are custom?
    – Cryalex
    May 16, 2014 at 3:00
  • Wow... that is what you were trying to say? Ok. Capabilities aren't hierarchical so far as I know. Why not create a helper function to parse your capability relationships? Then you aren't duplicating code "all over the place".
    – s_ha_dum
    May 16, 2014 at 3:13
  • Why not? Because it's not Wordpress standard. If I had to go and take workarounds at each difficulty, I would hack the core.
    – Cryalex
    May 16, 2014 at 17:53

1 Answer 1

public function myplugin_role_has_cap($capabilities, $cap, $args) {
    if(!isset($capabilities['myplugin_access'])) {
    return $capabilities;
}add_filter('user_has_cap', array($this, 'myplugin_role_has_cap'), 0 , 3);

Here is a better answer, that corresponds to my example. Let's say the plugin, on activation, adds the 2 caps to a role, and that after that for some reasons, the access cap is revoked. Then, even with only the delete_item cap, the user cannot delete, because access is not granted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.