Allowing Certain User Roles to View Plugin?

How do you allow certain user roles in WP to view a Plugin I am writing? Example would be I want the Administrator and Editor roles to see my plugin, but not Subscribers or Contributors?

Related posts

Leave a Reply

4 comments

  1. add your plugin with a capability argument.

    if your plugin entry point is an admin page menu, you can use something like this:

    add_menu_page(page_title, menu_title, capability, handle, [function], [icon_url]); 
    

    you can set the “capability” to “upload_files”. that function sees that capability argument as “The minimum capability required to display and use this menu page”

    for more info:

    http://codex.wordpress.org/Roles_and_Capabilities

  2. I don’t know whether there is a function for the roles specifically, but I know you can access that information using the usermeta API.

    So, for example, you can use

    $capabilities = get_usermeta( $user_id, "wp_capabilities" );
    

    to get the role of the user with id, $user_id. For a subscriber, it would return…

    Array
    (
        [subscriber] => 1
    )
    

    There may be a more elegant way of doing this, and there are role management plugins that can add more sophistication.

    Apparently get_usermeta will be deprecated in WP 3.0 in favor of get_user_meta(). You can read about that in the WP codex.

    I’m assuming that you need to control the visibility of the plugin in the front end UI, in which case you could get the role using the above and then make the UI elements conditional on the result.