Check if current user is administrator in wordpress

NEO picture NEO · Nov 6, 2013 · Viewed 68.6k times · Source

I am developing a plugin for wordpress, I want to find if current user is administrator or not, unfortunately I could not use the current_user_can() as it gives me error, so am using the global $current_user. But I could not get inside the if part even for admin user.. How to fix this?

global $current_user;
if ($current_user->role[0]=='administrator'){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type=\"text/css\"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options'  );
}

Answer

Gogol picture Gogol · Nov 6, 2013

Try something like the following:

if ( current_user_can( 'manage_options' ) ) {
    /* A user with admin privileges */
} else {
    /* A user without admin privileges */
}

Read more about the current_user_can function here.