1

I have a multisite WordPress installation set up, and have a user who is set to an 'Administrator' role on both sites. However, the function current_user_can('Administrator') does not return true when logged in as them, which I expected it to.

My user account, however, is an 'Administrator' on both sites, and also a 'Super Admin' on the network, and the call returns true for me.

Why is this? Shouldn't the first user only be false if checking for 'Super Admin' and not just 'Administrator'?

If this is an issue, is there any other way that I can check if a user is set to the 'Administrator' role without being a 'Super Admin'?

  • 2
    current_user_can() should never be used for a role check, especially on a multisite. It is generally just not reliable enough. Be aware that administrator is just a name, it doesn't say anything about the real capabilities. Test for capabilities, not for roles. – fuxia Aug 25 '15 at 23:16
4

I would agree that current_user_can('Administrator') should return true for the user. However, current_user_can is primarily intended to check for capabilities, which is generally a more robust check to be making.

I'd suggest something like this (untested):

if ( current_user_can( 'activate_plugins' ) && ! current_user_can( 'update_core' ) ) {
//...
}

So this is checking if the current user can activate plugins but cannot update core. This should return true for Administrators whilst returning false for Network Admins - as on multisite update_core is reserved for Network Admins.

| improve this answer | |
  • Even activate_plugins returned false, and I only now noticed that my admin user can't even edit plugins for the site that they are admin on, which seems strange. However, I did get this to idea to work using delete_pages instead. Thank you! – Blake Mann Aug 26 '15 at 14:38
  • Sorry, it's too early in the morning for me apparently.. delete_pages was far too open.. list_users is what I am using instead! – Blake Mann Aug 26 '15 at 15:21
8

There is a Typo in your expression use current_user_can('administrator') instead of current_user_can('Administrator') (a instead of A).

| improve this answer | |
  • While is this true, the other answer is still more correct when considering good practice. – Christine Cooper Nov 24 '17 at 13:03
  • 2
    This should be the accepted answer as it actually answers the OP's question, regardless of good practice. – SinisterBeard May 29 '18 at 13:20

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.