7

We know, that is_admin() checks if current URL belongs to DASHBOARD (BUT it doenst check whether user is ADMIN).

So, I use this function to detect if administrator is logged in wordpress:

function is_admin_user(){
  require_once(ABSPATH.'wp-includes/pluggable.php'); return current_user_can('create_users'); //or 'manage_options'
}

however, that is not ideal solution. Does there exist any built-in function, like wp_is_administrator()?

  • 2
    if(current_user_can('administrator')) – Howdy_McGee Aug 2 '13 at 17:06
  • 1
    What your code suggests is that you use some kinda bootstrap to laod WP functions outside WP but even in this case you can use what Howdy_McGee says -> see documenation – JMau Aug 2 '13 at 17:19
22

current_user_can will accept a role name but, sadly, the behavior with roles is not entirely consistent.

The following should work and is simpler than what you have, by a little bit.

$current_user = wp_get_current_user();
if (user_can( $current_user, 'administrator' )) {
  // user is an admin
}
3

It seems that the simplest way would in fact be to use current_user_can as such:

if( current_user_can( 'administrator' ) ){} // only if administrator

This seems like a duplicate.

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.