Check if current user is administrator in wordpress - php

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:
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.

Get the user and check if it has the role adminstrator, like so:
function is_site_admin(){
return in_array('administrator', wp_get_current_user()->roles);
}
if (is_site_admin()) {
echo 'Woot Woot';
} else {
echo 'So sad, I have no rights';
}

This works for me:
global $current_user;
if( !empty($current_user->roles) ){
foreach ($current_user->roles as $key => $value) {
if( $value == 'administrator' ){
Do Something
}
}
}
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.
$user_ID = get_current_user_id();
if($user_ID && is_super_admin( $user_id )) {
Do Something
}

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 usual undefined 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 to init. Following is one way of doing it.
/**
* Plugin Class
*/
class MyPlugin{
public function __construct(){
/* all hooks and initialization stuff here */
/* only hook if user has appropriate permissions */
if(current_user_can('manage_options')){
add_action( 'admin_head', array($this, 'hide_post_page_options'));
}
}
function hide_post_page_options() {
// 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_init', function(){
$myplugin = new MyPlugin();
});
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
$hide_post_options = <<<HTML
<style type="text/css">
.jaxtag { display: none; }
#category-adder { display: none; }
</style>
HTML;
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..
if(is_super_admin($user_id)) {
// do stuff for the admin user...
}
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.

<?php
if( current_user_can( 'administrator' ) ){} // only if administrator
if( current_user_can( 'editor' ) ){} // only if editor
if( current_user_can( 'author' ) ){} // only if author
if( current_user_can( 'contributor' ) ){} // only if contributor
if( current_user_can( 'subscriber' ) ){} // only if subscriber
?>
More info here: How To Check If User Is Administrator Or Editor In WordPress

use this code, I hope this solve your problem
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
echo trim($user_role);

$user=wp_get_current_user();
if(in_array("administrator", $user->roles)){
//user role is admin
}

Related

Format/Style current_user_display_name in WP

I am fairly new to coding and need a little guidance. I am using the following .php:
// [current_user_display_name]
function display_current_user_display_name () {
$user = wp_get_current_user();
$display_name = $user->display_name;
return $user->display_name;
}
add_shortcode('current_user_display_name', 'display_current_user_display_name');
and would like to style the text with CSS. I'm not sure how to do this, I tried this:
.display_current_user_display_name
{
color:#ffffff;
font-family:lato;
font-size:16px;
font-weight:regular;
}
but it did not work. Thanks!
This could be the PHP code for creating the WordPress shortcode.
Using this shortcode [current_user_display_name] will display an HTML span with the CSS class user-display-name.
We only show this if the user is logged in.
/**
* Current user display name shortcode.
*/
function get_current_user_display_name() {
// Check that the user is logged in.
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
return sprintf( '<span class="user-display-name">%s</span>', $user->display_name );
}
// If user is not logged in don't return anything.
return;
}
add_shortcode( 'current_user_display_name', 'get_current_user_display_name' );
Below is an example of how to style the CSS class .user-display-name:
.user-display-name {
/* CSS code here */
}

Hide billing fields Woocommerce for existing clients

I am trying to hide all checkout-fields for WP Woocommerce on the checkout page for clients who are logged in (so their info is already stored from previous orders). I am using this code, but I get errors for missing fields on pressing the checkout/finalize button.
/*remove billing fields for logged in users*/
add_filter( 'woocommerce_checkout_fields' , 'hide_billing_detail_checkout' );
function hide_billing_detail_checkout( $fields ) {
if( is_user_logged_in() ){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
I see the unset code is probably emptying everything. I would like all fields to be just hidden visually for logged in users. Any ideas?
After some testing I managed to get it done myself. For whoever needs it:
add_action( 'wp_head', 'include_styles' );
function include_styles() {
if ( is_checkout() ) {
if( is_user_logged_in() ){
echo '
<style>
.woocommerce-billing-fields {
display: none;
}
</style>
';
}
}
}

How do I add CSS only for certain user roles?

On all posts, I'm trying to hide the Update button, which is contained within this box:
<div id="postbox-container-1" class="postbox-container">
I only want to hide this box and button for a certain user role, 'Bookings Viewer', which I have created. Here is my code so far:
//Hides the update button for the Booking Viewer user role
add_action( 'wp', 'hide_update_booking_viewer' );
function hide_update_booking_viewer()
{
$user = wp_get_current_user();
if ( in_array( 'Bookings Viewer', (array) $user->roles ) ) { ?>
<style type="text/css" media="screen">
#postbox-container-1 {display:none;}
</style><?
}
}
Currently the code, which I've placed into functions.php, seems to have no effect. What am I doing wrong?
Try with a different hook:
function hide_update_booking_viewer() {
$user = wp_get_current_user();
if ( in_array( 'Bookings Viewer', (array) $user->roles ) ) {
'<style>
#postbox-container-1 {display:none !important;}
</style>'
} }
add_action( 'wp_head', 'hide_update_booking_viewer' );
This should output the styles into the head section of your webpage for the user "Bookings Viewer" if you registered the user role correctly.
But, as suggested in this post, it is better to rely on capabilities of the user rather than on it's name.
Example:
function hide_update_booking_viewer_1() {
if ( current_user_can( 'read' ) ) {
'<style>
#postbox-container-1 {display:none !important;}
</style>'
} }
add_action( 'wp_head', 'hide_update_booking_viewer_1' );
A list of capabilities and role types can be found here.
I've managed to work out the answer for myself - I was adding the action to the wrong hook. wp_head is for the 'front-end' website, so we need to use 'admin_head'. I also echoed out the CSS. Finally, '#publishing-action' refers to the Update button, which is what I was trying to hide specifically.
//Hides the update button for the Booking Viewer user role
function hide_update_booking_viewer()
{
$user = wp_get_current_user();
if ( in_array( 'bookings_viewer', (array) $user->roles ) ) {
echo('<style type="text/css" media="screen">
#publishing-action {display:none; !important}
</style>');
}
}
add_action( 'admin_head', 'hide_update_booking_viewer' );

Wordpress - current_user_can in functions.php

So I am trying to run a simple if statement inside the wp-functions.php file and am using current_user_can. However I get PHP errors like: "Fatal error: Call to undefined function current_user_can() in..."
If anyone could take a look at my code, that would be much appreciated.
The code I am using is here:
global $current_user_can;
if ( current_user_can( 'manage_options' ) ) {
/* Admin User */
} else {
/* Member */
echo "<p>something</p>";
}
This usually happens because pluggable.php is not loaded by some reason. Try to add this before your function;
if(!function_exists('wp_get_current_user')) { include(ABSPATH . "wp-includes/pluggable.php"); }
It just checks if pluggable is loaded, and if not, it includes it.
if you want to check directly the role of the member, you can use this code:
global $current_user;
get_currentuserinfo();
if( !in_array( 'administrator', $current_user->roles ) ) {
//Do something
} else {
//Do something else
}
if you are creating a plugin, you must have to include like
if(!function_exists('wp_get_current_user')) { include(ABSPATH . "wp-includes/pluggable.php"); }// no need to add for theme functions
global $current_user_can;
if ( ! current_user_can( 'manage_options' ) ) {
// your stuff here
}

In wordpress plugin check if user is admin before running a function

How do I check if a user is admin before running a function in a Wordpress plugin. Something that seems like it should be trivial is a pain.
I've looked at dozens of posts online and can't find one thing that works. I tried the following for example (among half a dozen other things) which is a function in a plugin :
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
function remove_post_metaboxes() {
remove_meta_box( 'formatdiv','album','normal' );
}
add_action('admin_menu','remove_post_metaboxes');
}
<?php if (current_user_can( 'manage_options' )) {
// do stuff
} ?>
$current_user = wp_get_current_user();
// print_r($current_user);
if ($current_user->has_cap('administrator')) {
// do something
echo 'is an admin';
}
So I was doing this wrong, the answer supplied by ReLeaf is partially correct but nobody pointed out that instead of trying to wrap the function like the example I gave in the original question and is why i was getting a blank admin screen :
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
function remove_post_metaboxes() {
remove_meta_box( 'formatdiv','album','normal' );
}
add_action('admin_menu','remove_post_metaboxes');
}
I should have had the conditional inside the function instead :
function remove_post_metaboxes() {
global $current_user;
if ( $current_user->role[0]=='administrator' ) {
remove_meta_box( 'formatdiv','album','normal' );
}
}
add_action('admin_menu','remove_post_metaboxes');
So that's how it's done, thanks me for pointing it out to me ;)

Categories