For a plugin I'm currently working on, I wanted to grant/restrict access to the plugin settings (meaning: the according admin menu pages) on a per role base.
Therefore I had to add a new plugin-specific capability to the user roles.
Unfortunately, kaiser's answer seems to be not working anymore, so I spent some time trying to figure out how to allow for the above mentioned functionality.
The Schedule
Before I share my code with you, here is what it's all about, in plain text:
- On plugin activation, add the new capability
THE_NEW_CAP to roles having a certain built-in capability (in my case: edit_pages).
- On each page load, do 1. (meaning: add the capability, again). This is only necessary if you want to account for possible new roles that have been created after the activation of the plugin. Hence, these new roles don't have the plugin-specific capability, even if they have the required built-in capability.
- Use the new capability for what you want. As explained before, I use it for granting/restricting access to the plugin's admin menu pages, so that is how its done in the following code example.
- On plugin deactivation, remove the capability. Of course, you could also do this when the plugin is being uninstalled. Either way, do it eventually.
The Code
And here is the above list converted into code:
» Setting It Up
class WPSE35165Plugin {
function __construct() {
// Register hooks
register_activation_hook(__FILE__, array(__CLASS__, 'activation'));
register_deactivation_hook(__FILE__, array(__CLASS__, 'deactivation'));
// Add actions
add_action('admin_menu', array(__CLASS__, 'admin_menu'));
} // function __construct
function activation() {
self::add_cap();
} // function activation
// Add the new capability to all roles capable of editing pages (or whatever)
private function add_cap() {
$roles = get_editable_roles();
foreach ($GLOBALS['wp_roles']->role_objects as $key => $role)
if (isset($roles[$key]) && $role->has_cap('edit_pages'))
$role->add_cap('THE_NEW_CAP');
} // private function add_cap
» Using It
// Add plugin menu pages to admin menu
function admin_menu() {
// Remove the following line if you don't care about new roles
// that have been created after plugin activation
self::add_cap();
// Set up the plugin admin menu
add_menu_page('Menu', 'Menu', 'THE_NEW_CAP', ...);
add_submenu_page('wpse35165', 'Submenu', 'Submenu', 'THE_NEW_CAP', ...);
} // function admin_menu
» Cleaning It Up
function deactivation() {
self::remove_cap();
} // function deactivation
// Remove the plugin-specific custom capability
private function remove_cap() {
$roles = get_editable_roles();
foreach ($GLOBALS['wp_roles']->role_objects as $key => $role)
if (isset($roles[$key]) && $role->has_cap('THE_NEW_CAP'))
$role->remove_cap('THE_NEW_CAP');
} // private function remove_cap
} // class WPSE35165Plugin