3

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?

6

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 Super Admin users (respectively) as soon as their Super Admin status changes.

Now as far as changing capabilities for existing Super Admin 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).

-1

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.

  • Hi, thanks for your answer, but super admins is not a role per se, so I cannot use this method – shea 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 – shea 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. – shea Jan 17 '13 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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