I want to restrict access of an user (ref. pic given)

DarKMaSk

Active member
Nov 17, 2020
306
78
43
Try this code:

PHP:
function remove_hoteller_from_admin_bar( $wp_admin_bar ) {
    $wp_admin_bar->remove_menu( 'hoteller_admin' );
}
add_action( 'admin_bar_menu', 'remove_hoteller_from_admin_bar', 2200 );

The theme adds this admin bar menu item with priority '2000'. To remove that action you have to set a priority that's higher in order to make it run AFTER the theme's action. And there you have your answer what that figure '2200' is for.
I got a very good teacher. 😍 This code is working like a charm. 😄
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
@frizzel Now I want to show you the full code of my 'restrict.php' file of 'mu-plugin'.

PHP:
<?php

/*
Plugin Name: MU-Plugin
Description: Must Use custom functions plugin
Author: frizzel
*/


/* $user_id = get_current_user_id();
if($user_id !== 1){
    // the code for all users except with ID 1

*/




function restrict_access(){
  $user = wp_get_current_user();
  $allowed_roles = array( 'editor', 'administrator', 'author' );
    if ( array_intersect( $allowed_roles, $user->roles ) ) {
       // the code for allowed users here
    }




function remove_hoteller_from_admin_bar( $wp_admin_bar ) {
    $wp_admin_bar->remove_menu( 'hoteller_admin' );
}
add_action( 'admin_bar_menu', 'remove_hoteller_from_admin_bar', 2200 );



show_admin_bar(false);

function customize_my_wp_admin_bar( $wp_admin_bar ) {
    $node = $wp_admin_bar->get_node('site-name');
    $node->meta['target'] = '_blank';
    $node->title = 'Home Page';
    $wp_admin_bar->add_node($node);
    $wp_admin_bar->remove_node('updates');
    $wp_admin_bar->remove_node('comments');
    $wp_admin_bar->remove_node('new-content');
    $wp_admin_bar->remove_node('wp-logo');
    $wp_admin_bar->remove_node('view-site');
}
add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 999 );

function wporg_remove_all_dashboard_metaboxes() {
    remove_action('welcome_panel','wp_welcome_panel');
    remove_meta_box('dashboard_primary','dashboard','side');
    remove_meta_box('dashboard_quick_press','dashboard','side');
    remove_meta_box('dashboard_site_health','dashboard','normal');
    remove_meta_box('dashboard_right_now','dashboard','normal');
    remove_meta_box('dashboard_activity','dashboard','normal');
    remove_meta_box('e-dashboard-overview','dashboard','normal'); // WooCommerce
    remove_meta_box('wpdm_dashboard_widget','dashboard','normal');

}
add_action( 'wp_dashboard_setup', 'wporg_remove_all_dashboard_metaboxes' );

function remove_screen_options(){
   return current_user_can( 'manage_options' );
}
add_filter('screen_options_show_screen', 'remove_screen_options');

function oz_remove_help_tabs( $old_help, $screen_id, $screen ){
    $screen->remove_help_tabs();
    return $old_help;
}
add_filter( 'contextual_help', 'oz_remove_help_tabs', 999, 3 );

add_action("admin_head" , function(){
   echo "<style>#dashboard-widgets-wrap .empty-container{display:none}</style>";
});



// Customizing the default WordPress logo on the Admin Header

function add_my_own_logo( $wp_admin_bar ) {
    $args = array(
        'id'    => 'my-logo',
        'meta'  => array( 'class' => 'my-logo', 'title' => 'logo' )
    );
    $wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'add_my_own_logo', 1 );



// Customizing the Default footer of Admin Dashboard
function remove_footer_admin () {
return 'Welcome to SD Hotel Booking System';
}
add_filter('admin_footer_text', 'remove_footer_admin', 99);

function remove_footer_update () {
return 'Version 1.0';
}
add_filter('update_footer', 'remove_footer_update', 99);

// CSS for styling and path of my own logo instead of default WordPress Logo on the Admin Header

/*#wp-admin-bar-my-logo div{
    background-image: url( /path/to/your/logo.png );
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: center;
    margin-left:10px!important;
} */
}
add_action('init','restrict_access');

Please let me know if I placed it correctly or not.

Beside this, I have a new problem. When I log in with this new user-role 'Admin' (not the Administrator), I can see all the bookings, but I cannot edit them. See the screenshots below.

'Booking' dashboard as Administrator:

1. Image 1 - Administrator Dashboard of Booking:

administrator-dashboard-for-booking.jpg

2. Image 2 - Administrator Dashboard of Edit Booking:

administrator-dashboard-for-booking_edit.jpg

3. Admin (i.e. the custom user-role which we are working on) dashboard of Booking:

admin-dashboard-for-booking.jpg

Now if you compare Image 1 & Image 3, you can clearly see that the Red Marking Areas are missing, and as those are missing there is no 'Edit' dashboard like Image 2 in 'Booking' menu and in any of its sub-menus. Now how can we get back those option for this user-role? The 'Booking' and all its sub-menu should be exact like those are in 'Administrator Dashbord', otherwise you can understand this total hard work (obviously yours) will all in vain.
 
Last edited:

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
My guess is that you have not given the Admin enough rights in the plug-in you use.

Why did you change the code from the ID check to the user role check? I wrote: "Since you only have one other user, I'd go for the restrictions by ID any time". I wish I hadn't given you the answer to why it triggered an error, but instead would have given you the advice to simply forget about it. Because: it is very inefficient to put functions inside another function. You should NEVER do that, so change the code back to what it was with just a simple 'if' statement.

Keep the restrictions by user role in the plug-in only.
 
  • Like
Reactions: DarKMaSk

DarKMaSk

Active member
Nov 17, 2020
306
78
43
My guess is that you have not given the Admin enough rights in the plug-in you use.
I have to check.

Why did you change the code from the ID check to the user role check? I wrote: "Since you only have one other user, I'd go for the restrictions by ID any time". I wish I hadn't given you the answer to why it triggered an error, but instead would have given you the advice to simply forget about it. Because: it is very inefficient to put functions inside another function. You should NEVER do that, so change the code back to what it was with just a simple 'if' statement.
Ok. Now this is the clean code of my 'restrict.php' file:
PHP:
<?php

$user_id = get_current_user_id();
if($user_id !== 1){
function remove_hoteller_from_admin_bar( $wp_admin_bar ) { $wp_admin_bar->remove_menu( 'hoteller_admin' );
}
add_action( 'admin_bar_menu', 'remove_hoteller_from_admin_bar', 2200 );
show_admin_bar(false);
function customize_my_wp_admin_bar( $wp_admin_bar ) { $node = $wp_admin_bar->get_node('site-name'); $node->meta['target'] = '_blank'; $node->title = 'Home Page'; $wp_admin_bar->add_node($node); $wp_admin_bar->remove_node('updates'); $wp_admin_bar->remove_node('comments'); $wp_admin_bar->remove_node('new-content'); $wp_admin_bar->remove_node('wp-logo'); $wp_admin_bar->remove_node('view-site');
}
add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 999 );
function wporg_remove_all_dashboard_metaboxes() { remove_action('welcome_panel','wp_welcome_panel'); remove_meta_box('dashboard_primary','dashboard','side'); remove_meta_box('dashboard_quick_press','dashboard','side'); remove_meta_box('dashboard_site_health','dashboard','normal'); remove_meta_box('dashboard_right_now','dashboard','normal'); remove_meta_box('dashboard_activity','dashboard','normal'); remove_meta_box('e-dashboard-overview','dashboard','normal'); // WooCommerce remove_meta_box('wpdm_dashboard_widget','dashboard','normal');
}
add_action( 'wp_dashboard_setup', 'wporg_remove_all_dashboard_metaboxes' );
function remove_screen_options(){ return current_user_can( 'manage_options' );
}
add_filter('screen_options_show_screen', 'remove_screen_options');
function oz_remove_help_tabs( $old_help, $screen_id, $screen ){ $screen->remove_help_tabs(); return $old_help;
}
add_filter( 'contextual_help', 'oz_remove_help_tabs', 999, 3 );
add_action("admin_head" , function(){ echo "<style>#dashboard-widgets-wrap .empty-container{display:none}</style>";
});
function add_my_own_logo( $wp_admin_bar ) { $args = array( 'id' => 'my-logo', 'meta' => array( 'class' => 'my-logo', 'title' => 'logo' ) ); $wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'add_my_own_logo', 1 );
function remove_footer_admin () {
return 'Welcome to SD Hotel Booking System';
}
add_filter('admin_footer_text', 'remove_footer_admin', 99);
function remove_footer_update () {
return 'Version 1.0';
}
add_filter('update_footer', 'remove_footer_update', 99);

}

Keep the restrictions by user role in the plug-in only.
Ok. Understood.
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
My guess is that you have not given the Admin enough rights in the plug-in you use.
I have solved the problem. As you advised me to keep the restrictions in the plug-in only, I did that. And in 'Menu Editor Pro', what we have to do for effectively restrict certain user role to access or restrict some features is to go to Roles > Admin (i.e. the Custom User role). See the pic below:

menu-editor-pro-options.jpg

And, this is the current user dashboard looks like 😊:

custom-user-dashboard.jpg


The function of this Mu-Plugin

This 'mu-plugin' is responsible for the cleaner look of this custom user dashboard, which includes the clean Header, removal of WordPress logo and all the nodes related to it, removal of Screen Option and Help tab below the Header, removal of Metaboxes, change of Footer.

To achieve this, ALL CREDIT GOES TO @frizzel and that's why I have dedicated this 'mu-plugin' to him by putting his name as the AUTHOR OF THIS PLUGIN. He is THE ONE who have guided me all along. If anyone wants this 'mu-plugin', please feel free to ask and I would upload the file here. The only thing I would request to do whoever gets it, PLEASE DO NOT CHANGE THE AUTHOR NAME as it is a way to showing the respect to the person who did all the hardwork and taught me how to do this.

Thank you all who have contributed to this thread and special thanks to @frizzel for not becoming impatient and guiding me through all my questions and errors to the success. Thank you my friend... thanks a lot. ❤️

Disclaimer: The 'Welcome Aboard' section is not part of the 'mu-plugin' and 'Menu Editor Pro' and it is on the making.
 
Last edited:
  • Like
Reactions: Mscv50

DarKMaSk

Active member
Nov 17, 2020
306
78
43
I forgot to show the pic of the User Dashboard after I configured those options in 'Menu Editor Pro'. Here it is:

after-configuring-menu-editor-pro.jpg
You can see all the options are available now.
 

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu