$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>if admin is logged in|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

if admin is logged in

matteradmin10PV0评论

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()?

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()?

Share Improve this question edited Nov 9, 2017 at 21:34 T.Todua asked Aug 2, 2013 at 16:46 T.ToduaT.Todua 5,8909 gold badges52 silver badges81 bronze badges 2
  • 3 if(current_user_can('administrator')) – Howdy_McGee Commented Aug 2, 2013 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 Commented Aug 2, 2013 at 17:19
Add a comment  | 

2 Answers 2

Reset to default 25

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
}

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far