Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a plugin that uses custom capabilities. Some of those capabilities need to be added to all users who are super admins. Currently, I'm using this code:

$supers = get_super_admins();
foreach ( $supers as $admin ) {
    $user = new WP_User( 0, $admin );
    $user->add_cap( 'my_cap' );
    $user->add_cap( 'my_second_cap' );
}

However, I'm concerned that this code is just adding the capabilities to the individual users, as opposed to the role as I usually would. If a new super admin is created, they will not have these capabilities as this function only runs on plugin activation.

Is there a better way of adding custom capabilities to super admins?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Although this isn't well documented, "Super Admin" is not a role (in that it is not an actual role object). It's more like a special "status".

A list of users who are Super Admins (also called "network admins" or "site admins") are stored in a database site-option record called 'site_admins'. Generally, adding a capability to the Administrator role is enough since Super Admins are also, de facto, members of Administrator role with all it's capabilities.

That said, if you specifically need to add a capability only to Super Admins (but not "normal" administrators), it might be better to simply use WordPress's "is_super_admin()" function instead of using capabilities at all, since it is assumed that Super Admins have no restrictions.

If you really need to use capabilities, you should use the 'grant_super_admin' and 'remove_super_admin' hooks to add or remove capabilities to/from SuperAdmin users (respectively) as soon as their SuperAdmin status changes.

Now as far as changing capabilities for existing SuperAdmin users, your approach is the best... fetch a list of super admin users, loop through it, and add capabilities to each (although should only need to run that once EVER if you used the above hooks).

share|improve this answer
1  
Very nice, I didn't know about those hooks. –  bungeshea Feb 15 '13 at 20:13

The things to do:

  1. Get the author role.
  2. Add capability to that role.

For example:

function add_theme_caps() {
    // gets the author role
    $role = get_role( 'author' );

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    $role->add_cap( 'edit_others_posts' ); 
}

add_action( 'admin_init', 'add_theme_caps');

The code above is a snippet from Function Reference/add_cap.

share|improve this answer
    
Hi, thanks for your answer, but super admins is not a role per se, so I cannot use this method –  bungeshea Jan 17 '13 at 8:40
    
Also, the Codex page you linked to recommends: This setting is saved to the database, so it might be better to run this on theme/plugin activation –  bungeshea Jan 17 '13 at 8:40
    
I removed my recommendation from my answer, I didn't notice that part in the Codex page. But why wouldn't you make a role? –  Mike Madern Jan 17 '13 at 8:49
    
I do not want to create a custom role - I want to assign custom capabilities to all super admins. However, 'super admin' is not a role, and while the code I'm using works, I'm concerned if it's the best method. –  bungeshea Jan 17 '13 at 9:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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