Is there a way to do this without using another plugin and without modifying WP core code:

I want to give the custom plugin I am writing a settings page. So I will need to add plugin capabilities to the role that uses my plugin. However, I do not want those users to see or be able to change settings for any other plugins that might be installed. Just my plugin.

I was hoping there was something like $role_object->add_cap('update_settings_myPlugin'), similar to the way capabilities can be mapped to roles for custom post types.

  • This answer by tfrommen should get you started! – Scriptonomy Nov 10 '15 at 1:55

Man. This was not easy to chase down. The answers up above absolutely do not work unless you give the user's role very broad "manage_options" capability. I asked (and answered) the Trac issue here: Admin custom option screen not saved unless user manage_options capability

Anyone in the future who might search/read this - the trick is to leverage that new filter capability to register your required capability for the more narrow "manage options" activity for your specific plugin or theme.

|improve this answer|||||

If I understand you, what you are thinking about doing should work. add_menu_page() or related submenu and settings page functions, accept an argument for required capability. For example:

add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );

The third parameter is "The capability required for this menu to be displayed to the user." Set that to some capability of your own invention, then add that capability to your user:

$user = new WP_User( $user_id );
$user->add_cap( 'can_edit_posts' );

Or to a role:

// get the the role object
$role_object = get_role( $role_name );

// add $cap capability to this role object
$role_object->add_cap( $capability_name );
|improve this answer|||||
  • yes, this is exactly what I was hoping for - thank you. – C C Nov 10 '15 at 2:58
  • Unfortunately, once I implemented this (and checked with the "this might be a duplicate" answer as well) -- it does not work. It appears that the role also must have capability "manage_options" added to it, otherwise upon saving the options for my plugin I get the "Cheatin 'uh?" error in WP. I think adding this capability to the role will expose all other theme or plugin options to my users, which is exactly what I don't want to do. – C C Nov 10 '15 at 17:30
  • I'm hitting the same issue found here, but for my plugin not a theme. Granting "manage_options" is way too broad, just to allow my plugin settings page to save its options back to the database. core.trac.wordpress.org/ticket/14365 – C C Nov 10 '15 at 17:44
  • I'd have to see your implementation – s_ha_dum Nov 10 '15 at 17:44
  • It's very similar to: wordpress.stackexchange.com/questions/35165/… -- I just add a custom capability to the role, and only show the new admin menu if the user has that role/capability. However unless the role also has "manage_options" capability (which opens up a huge can of worms) - the "Submit" action of my new options page in Admin will fail. So that Trac I linked is exactly my problem and I don't see that it was ever fixed by WP. – C C Nov 10 '15 at 18:15

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