27 responses to “Custom capabilities in plugins and themes”

  1. Catherine Jacobs

    Thank you this will come in handy.

  2. johnbillion

    You could also achieve the same thing without filters (not because it’s better or worse, just different).

    function my_plugin_capability(){
        if ( function_exists( 'members_get_capabilities' ) )
            return 'edit_my_plugin_settings';
        else
            return 'manage_options';
    }
    add_submenu_page( $parent_page, $page_title, $menu_title, my_plugin_capability(), $file, $function );

    Haven’t had a chance to properly try out this plugin yet Justin but it looks very promising. Keep up the good work.

  3. johnbillion

    I’d like to suggest an alternative method for this which is simpler and potentially allows effortless compatibility with other role management plugins. I’m not that good at explaining things so bear with me :)

    The aim is to get rid of the if ( function_exists( '...' ) ) checks inside plugins that want to utilise custom capabilities. It makes compatibility with other role management plugins a pain, as you need a check for each role management plugin that’s available. Not ideal.

    To avoid this, one common filter could be used (in an ideal world this would be used by all role management plugins) that gets filtered by the currently installed role management plugin, not by the plugin that wants to use custom capabilities.

    add_submenu_page( $parent_page, $page_title, $menu_title, apply_filters( 'use_custom_capability', 'manage_options', 'edit_my_plugin_settings' ), $file, $function );

    Note the parameters in the call to apply_filters. The usual parameter is the default capability that will get used when no role management plugin is used. The second is the capability that will get used if there is one (eg. if Members is installed).

    This method requires the following addition to the Members plugin, but once added would form a solid system for allowing plugins to utilise custom capabilities if any role management plugin such as Members is installed.

    function use_custom_capability( $default, $custom ) {
    	return $custom;
    }
    
    add_filter( 'use_custom_capability', 'use_custom_capability', 10, 3 );

    This function simply returns the custom capability, nothing else. If this code was used in other role management plugins, plugins would isntantly have compatibility with all the role management plugins without needing to perform a check for each one (the if ( function_exists( '...' ) ) stuff above).

    Finally, with regard to Adding the capability to the role edit page, if this filter was given a more generic name (such as ‘get_capabilities’ if that doesn’t already exist) then other role management plugins could use this method for adding custom capabilies to a role administration screen.

    Suggestions?

    (P.S. a Preview button on your blog comment box would be great)

  4. johnbillion

    Your code makes the assumption that only one admin menu page that only needs a single capability exists for the second plugin.

    Hmm, not following you here.

    apply_filters( 'use_custom_capability', 'manage_options', 'edit_my_plugin_settings' )

    The above code could be used multiple times in a plugin wherever a capability check occurs. Eg.:

    // admin menu 1:
    add_submenu_page( $parent_page, $page_title, $menu_title, apply_filters( 'use_custom_capability', 'edit_posts', 'edit_my_plugin_description' ), $file, $function );
    // admin menu 2 with higher permissions:
    add_submenu_page( $parent_page, $page_title, $menu_title, apply_filters( 'use_custom_capability', 'manage_options', 'edit_my_plugin_settings' ), $file, $function );
  5. johnbillion

    Obviously my suggestion is also an ideal world solution, where every role management plugin uses the same filter and every plugin that wants to allow custom capabilities does the same! I don’t think OSS projects are renowned for having widely implemented standards.

    I think my brain has been fried for the past few days.

    Tell me about it. I’m surprised anything I say at the moment makes sense. I’m working with multiple custom taxonomy intersections in queries (and hoping a patch for WP will come out of it all).

  6. CG

    could this be made into a plugin so that different users can be limited to see a different back end… depending on their role?

  7. CG

    I agree… I know of a plugin that ‘hides’ some menu items but it does so for everyone including the admin.. pity there’s not something that can hide menus according to XYZ.. that would be ideal…

  8. Steve @ Annuity

    Thank you for the plugin. It should be very useful with custom capabilities.

    Thanks,
    Steve

  9. Alfred

    I’m going to sound like a jerk for asking, but as a computer illiterate who likes blogging, is there an easier way to do this? By easier I mean… less hands on…

  10. MacBoy

    Without modifying a plugin, what code can I drop into my theme’s functions.php file to get the same net effect?

    I have a dual-goal:

    1. Don’t modify plugins to stay in sync with changes.

    2. Restrict (allow) each role to the necessary set of capabilities provided by core WP and by each plugin

    Perhaps the theme-based approach via functions.php might be a reasonable approach. From your example code, is all that “droppable” into functions.php ?

  11. oliver

    Thank you for making our lives simple and easy…. i liked this plugin and would love to use it on my website.

  12. Michael Langham

    Most excellent plug-in Justin.

    Just ran it through its paces and not only was it easy to set-up, but the action/alert items right after activation were helpful reminders.

    Quick question– I’m also utilizing the Calendar plug-in. It has a feature that offers the admin to designate which lowest user rating has access to the calendar.

    After creating a role via Members, it was not recognized via Members.

    I’d like ultimately to offer editing of the calendar items only, with no other posting/editing access to certain users.

    I’m not sure after reading this article if this is something that can be set via your plug-in. Is it just too late on a Friday for me to have grok’d it, or can you point me in the right direction.

    Thanks again for an Excellent plug-in offering. It’s already gone into my folder labeled “must-have-plugs”, and has also gone on my writing list of items to review in my blog.

    Have a great weekend.

    Michael

  13. Michael Langham

    Ack:

    After creating a role via Members, it was not recognized via Members.

    I meant to say “…it (role) was not recognized by Calendar”

    Like I said, too late on a Friday night.
    Thanks again.

  14. leen

    Hi,thanks fot yoy MEMBER plugin!

    Making private pages is a piece of cake now!
    I do have 1 question tough: I want to make a detail (not private) page that can be updated by one user only (with specific role).
    So I make a capability ‘edit_member1_page’. But this doesn’t work. Do you have any suggestions..? thank you very muc,
    leen

  15. Michael Langham

    Hello again Justin.

    The ‘calendar plug-in’ author has responded with the following info with regard to your suggestion that they employ “$wp_roles variable or the WP_Roles class rather than assuming that everyone uses the default roles” in your above reply to me.

    For perusal, should you or others enjoy, is the link to the forum post:

    View topic – Is Update planned to utilize WordPress roles system? http://j.mp/6n5rY

  16. Michael Langham

    OOpah.

    Here was his response:

    Kieran wrote:
    Possibly. The next major version will see an improvement to the permissions system, whether it will integrate with roles or not is a different matter. The more API documentation is provided to me in this thread, the more likely it is – I only understand the default permissions in WordPress at this point, hence their use in the plugin.

  17. Michael Langham

    I guess I should have actually posed the question to this thread as to

    “Where might Tieren get more information regarding codex for his plug-in’s revision so as to include “$wp_roles variable or the WP_Roles class” per Justin’s suggestion.

    I’ll do the legwork for him if for no other reason than to pay-it-forward.

    Thanks in advance.

    Michael

  18. Melody: Sanitary Connector

    Thanks for walking through the capabilities of your plug-in. Frankly, I have a moderate amount of technical knowledge (probably just enough to get into trouble), but your direction was quite clear. I maintain a blog with about a dozen writers some of whom really create a mess. I plan on looking more closely at using your plug-in to prevent some recent nightmares.

  19. Eurolappen @ Fahrschule Tschechien

    Again a very great plug in. Thank you for sharing your great talent and knowledge with us.

  20. Adam

    that was a great plugin. I love how it works and my viewers like it too. I got a few comments on the new plugin on a couple of my sites.

  21. TOEIC

    It looks great but my concern is… what would your wordpress become? Would your scipts be overwritten by any new auto updates you may want to do? How do you agree those things

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (e.g., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.