Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');

global $current_user;
get_currentuserinfo();
$user_info= get_userdata( $current_user->ID );
$lvl=$user_info->user_level;     
     if ($lvl == '8' || $lvl == '9' || $lvl == '10')
     {
     echo 'You are admin';
     }
?>

doesnt there exist the easier way?

share|improve this question
1  
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
add comment

1 Answer 1

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
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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