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' );
}
Try something like the following:
Read more about the
current_user_can
function here.Get the user and check if it has the role adminstrator, like so:
This works for me:
If it’s not a multi-site set-up, you can use this to detect an administrator. If it’s multi-site, this will only return true for a super admin.
I know it is an old question but I would like to make this page more useful by addressing the actual issue. The actual issue here is that OP hasn’t been able to use
current_user_can( 'manage_options' )
in his plugin. Using the function raises the usualundefined function...
PHP error. This happens because the plugin gets initialized before WP core completes loading. Fix is very simple. Loading the plugin at appropriate time is the key.Assuming the admin plugin code resides inside a class
MyPlugin
, the class initialization should be hooked toinit
. Following is one way of doing it.This is a way of making sure that wordpress core is available to the plugin function.
You can find
admin_init
documentation here.P.S. You should look into using PHP HEREDOC. It is a very simple way of writing multi-line strings. Your style block can be re-written as follows
I hope it helps somebody.
Thanks.
Too late for an answer for this question, but I think it might be useful anyway if someone ends up here like me.
I needed a quick solution to this problem – check if the current user is admin or not.
From the WP codex I got a simple solution which is to use..
According to WP-Codex this function returns True if the currently logged in user is network (super) admin. This function returns True even if the network mode is disabled but the current user is admin.
More info here: How To Check If User Is Administrator Or Editor In WordPress
use this code, I hope this solve your problem