1
  protected function isAdmin() : bool {
    $check = current_user_can('administrator');
    return $check; 
    // returns false as I am logged in as superadmin, refreshing the page for xdebug to run
  }

$check also evaluates to false when using current_user_can('delete_site');

My wordpress install is hosted on my local machine, Lubuntu 18.04 with apache2. Php 7.2. I can access the site by navigating to http://localhost/folderName in Google Chrome.

I have cookie auth enabled and see my user cookie is present with the EditThisCookie Chrome extension.

In the xdebug console (of Phpstorm 2019.2) I also get a false response:

current_user_can('delete_site');
false

‌‌current_user_can('administrator');
‌false

I have no issues navigating around the front or back end of the wordpress site as an Admin. I feel like this might be an environment issue due to the discrepency betwen the Chrome experience and what the plugin and Xdebug are catching.

Does anyone know why I am running into this issue?

  • Where are you running this code? If you’re running it too early the current user won’t be set. – Jacob Peattie Sep 2 at 3:34
  • @JacobPeattie It is being run in a rest api request handler callback. The admin user clicks on a delete icon next to a table row which executes the request. the request handler is hooked as an action to rest_api_init – Sean Dezoysa Sep 2 at 11:25
  • 1
    You need to send a nonce with REST requests for the current user to be recognised: developer.wordpress.org/rest-api/using-the-rest-api/… – Jacob Peattie Sep 2 at 15:12
  • @JacobPeattie the nonce solution turned out to be the right one, thank you – Sean Dezoysa Sep 2 at 15:22
1

Normally for checking for administration privileges you have to check the “manage_options” capability, such as:

current_user_can('manage_options')

Alternatively, you want to list the roles with wp_get_current_user()->roles and ensure the “Administrator” role is in that array.

The capabilities you are checking for do not exist in a vanilla install of wp.

  • Thank you, I tried using manage_options but it still returned false. Then I ran wp_get_current_user()->roles; and got back an empty array. Any idea why this is happening? – Sean Dezoysa Sep 1 at 21:14
  • Can you please var_dump(wp_get_current_user()) and confirm you are logged in? Also, in case it's a MU install, try checking against the "manage_network" permission. – santamanno Sep 1 at 21:44
  • If you have more to add that can help us write good answers please edit your question and update us. – Matthew Brown aka Lord Matt Sep 1 at 23:45
  • I can't seem to var dump it: PHP Fatal error: Uncaught Error: Call to undefined function admin\controllers\‌var_dump() in <path to php file> on line 117. The debugger shows an empty array on $roles when I add the following line to the auth checking callback: $roles = wp_get_current_user()->roles; Now I'm looking into the reasons why the current WP_User object shows blanks or 0's for all properties. – Sean Dezoysa Sep 2 at 11:33
  • 1
    Ok, I needed to pass the current user data from the server/PHP to the browser for the React app. When I ran wp_get_current_user(); in the right place it worked – Sean Dezoysa Sep 2 at 15:21

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.