WordPress.org

Ready to get started?Download WordPress

Forums

Members
Best way to detect Members plugin (4 posts)

  1. Ben Huson
    Member
    Posted 2 years ago #

    Hello,

    I'm writing a plugin that adds new capabilities and only want to assign them to certain roles by default.

    What is the best way to make this compatible with the Members plugin so that I can assign my capabilities to some default roles, but from then on manage them using the Members plugin?

    Just want to be sure that when my plugin is updated/actiavted in future, it won't overwrite Role > Capabilities that Members has assigned.

    Any ideas?

    - Ben

    http://wordpress.org/extend/plugins/members/

  2. Justin Tadlock
    Member
    Plugin Author

    Posted 2 years ago #

    If you're adding capabilities on plugin activation (as you should be), it will integrate just fine with Members. The caps will automatically be checked for the roles that have them and be available to select for roles that don't have them.

    Members is just an interface for the roles/caps system in WordPress. It'll work with any plugin that's using this system correctly.

    The biggest issue I've seen with plugins that it doesn't work with is when the plugin tries to change capabilities on every page or every admin page because this overwrites what the user selected (and is horrible for performance). So, make sure not to hook to init or admin_init or anything like that. Register an activation hook for your plugin and set up custom capabilities only once on this hook.

  3. Ben Huson
    Member
    Posted 2 years ago #

    Thanks Justin,

    That makes sense. I guess I should do a check on activate to see if any of the caps already exist on the admin role otherwise when the plugin is upgraded or de-activated and re-activated it would repopulate the default roles, overriding any changes that may have been made via the Members plugin. Am I wrong in thinking the activation hook gets fired when a plugin is upgraded via the admin?

    I have noticed many plugins making the mistake of adding roles/caps on every initial. I think this is partly down to the documentation on the codex which doesn't make it clear - I think the example it gives even uses the unit hook! I didn't realise myself until I tested it in practice. I assumed that maybe capabilities worked like a filter where they were defined on each page load.

    Think the Codex may need a little TLC :)

  4. Justin Tadlock
    Member
    Plugin Author

    Posted 2 years ago #

    Actually, checking if the cap is assigned is a really good idea on activation. That way, you avoid overwriting changes when a plugin is deactivated then reactivated.

    There's the has_cap() method on WP_Role, so doing that check should be fairly easy.

    $role =& get_role( 'administrator' );
    if ( $role->has_cap( 'something' ) )

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags