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' );
Related
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>
';
}
}
}
So its about hiding widgets for specific user roles excluding Admin.Using custom sidebar plugin i don't want to be display on users end.Listed all Dashboard widgets through
function list_active_dashboard_widgets() {
global $wp_meta_boxes;
foreach (array_keys($wp_meta_boxes['dashboard']['normal']['core']) as $name) {
echo '<div>' . $name . '</div>';
}
}
add_action('wp_dashboard_setup', 'list_active_dashboard_widgets');
and found customsidebars-mb
What i am doing without succes is adding this code to hide sidebar options widget from users panel keeping it on admin panel.
function disable_default_dashboard_widgets() {remove_meta_box('customsidebars-mb', 'dashboard', 'normal');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'disable_default_dashboard_widgets');
}
**
Store front Theme Woocommerce Plugin
**Will be phasing out all plugins with codes
You can try this one in your functions.php,
add_action( 'widgets_init', 'remove_widgets_wpse_89138' , 15 );
function remove_widgets_wpse_89138()
{
// http://codex.wordpress.org/Function_Reference/is_admin
if( !is_admin() )
return;
// Grab current user info
global $current_user;
// Check for specific user
/*
$username = $current_user->user_login;
if( 'the_user_login' != $username)
return;
*/
// Check for capability
if( current_user_can( 'add_users' ) )
return;
unregister_widget( 'WP_Widget_Pages' );
}
Hope this will helps you. For more please visit, URL 1, URL 2
I have a wordpress website, its using buddypress and bbpress. I need to hide/redirect all the buddypress and bbpress pages from people who are not logged in. So if someone lands on the members page, profile page or any forum topic it needs to redirect them to the signup page.
I tried maybe 5 plugins, all of them caused issues like 404 errors, not working or just white pages.
The url structure is like this:
www.example.com/members
www.example.com/members/luke
www.example.com/forums
www.example.com/forums/forum/general-chat
Does anyone know how I can do this without a plugin?
you have to modify from within a child theme the profile-loop.php file
your-child-theme/members/single/profile/profile-loop.php
On the first line of the file, add
<?php if ( is_user_logged_in() ) : ?>
At the end of the file, insert between the last endif and the last do_action this:
<?php else : ?>
<?php echo “<div style=’width: 600px;height:25px; padding: 4px; border: 3px solid #ff0000; text-align: center; font-style:bold; font-size: 1.3em;’> You must be logged in to view a member profile</div>”; ?>
<?php endif; ?>
Change the div inline style to whatever you need accordingly to your theme. The example fits with bp-default.
If you can not do that, then try this plugin,
plugin
Try this but make sure to change the url to what you want
add_action( 'admin_init', 'redirect_non_logged_users_to_specific_page' );
function redirect_non_logged_users_to_specific_page() {
if ( !is_user_logged_in() && is_page('add page slug or i.d here') && $_SERVER['PHP_SELF'] != '/wp-admin/admin-ajax.php' ) {
wp_redirect( 'http://www.example.dev/page/' );
exit;
}
Try this in your theme/functions.php or in bp-custom.php:
function lukedi_private_check() {
if ( ! is_admin() && ! is_user_logged_in() ) {
if ( is_front_page() || is_home() || bp_is_register_page() || bp_is_activation_page() )
return;
$redirect_url = trailingslashit( site_url() ); // change this to whatever you need
// member page
if ( bp_is_user() )
bp_core_redirect( $redirect_url );
// bbPress
if( is_bbpress() )
bp_core_redirect( $redirect_url );
// members loop
$bp_current_component = bp_current_component();
if ( false != $bp_current_component ) {
if ( 'members' == $bp_current_component )
bp_core_redirect( $redirect_url );
}
}
}
add_action( 'bp_ready', 'lukedi_private_check' );
Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks
In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
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
}