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

DarKMaSk

Active member
Nov 17, 2020
306
78
43
  • Title for your fault: Want to make a custom role to restrict an admin to access certain area
  • Fault Description: I am unable to create a Custom Role with which I can restrict an admin to access certain areas in Admin Dashboard of WordPress.
  • What have you done to try to fix the issue: I am not a coder but I do edit some code by Google-ing. I tried to make a MU-PLUGIN which remained unsuccessful. I tried to add functions in the site theme's functions.php file but could not make it work as per my desire.
  • What I want to do: I am building a hotel website with wordpress. Now, I have to give my client the admin access to manage the booking and accommodation only because he is not a developer or designer. He is the owner-manager of his small hotel. So, if I give him full admin access, he will be overwhelmed and might mess up with the design and functionality of the website. To do that, I want all menus / options on the left side panel will be inaccessible and hidden except the booking and accommodation option. I prefer using a method which will not be destroyed / changed with the update of the theme. I have attached a screenshot with multicoloured markings and their explanations for better understanding. So far, I have successfully hide the rest of the menu / links / options on the left panel using some code in functions.php file of the theme I used but have failed to hide the red-marked area Can you guys help me? Please guide me.

Note: I don't know if I update the theme, will it destroy my code in the functions.php file? If yes, then how can I prevent that? Please suggest this also.

Reference image:

restrict-access.jpg
 
Last edited:

MrSam_1

Well-known member
Administrative
Trusted Seller
Dec 1, 2018
24,129
27,395
120
I don't know if I update the theme, will it destroy my code in the functions.php file? If yes, then how can I prevent that?
That's why you use child themes. Any changes made to child themes remain there in case of main theme updates.

For your issue I know there was a plugin that was restricting access to backend functionalities but I don't really remember its name.
 
  • Like
Reactions: DarKMaSk

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
Yes, you need to add code in functions.php of a CHILD THEME or you can use a Code Snippets plug-in.

But what I don't understand is you're saying:
  • Fault Description: I am unable to create a Custom Role with which I can restrict an admin to access certain areas in Admin Dashboard of WordPress.

What do you mean? Of course you can give him a role and disable access to all menu items except Accommodation and Booking. This plug-in can do that:

As for the Dashboard and admin menu bar, here's the code to make it blank:
PHP:
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>";
});
 
  • Like
Reactions: DarKMaSk

DarKMaSk

Active member
Nov 17, 2020
306
78
43
Yes, you need to add code in functions.php of a CHILD THEME or you can use a Code Snippets plug-in.

Thank you. I also thought about that. But do I have to copy all the codes from the functions.php of the Theme to the Child Theme's functions.php file?

But what I don't understand is you're saying:
Fault Description: I am unable to create a Custom Role with which I can restrict an admin to access certain areas in Admin Dashboard of WordPress.
What do you mean?
Actually. I wanted to say that I could not do it properly, you saw the result in the attached image. It is not WordPress's fault but it is my fault that I could not do it. But, now I am sure I would be able to do it with the help of the friends like you. Thanks a lot, but I might need help again.

Of course you can give him a role and disable access to all menu items except Accommodation and Booking. This plug-in can do that:

Thanks for the plugin's link. I would try it definitely, though I am trying not to use anymore plugin because there are 19 plugins already installed and all are required by the Theme although WooCommerce and its related plugins and MailChimp are not required for my site's functionality but I don't know how to get rid of them because if I disable them, the theme always asks to install them again. Those extra plugins are real burden. So, I am trying to use the functions.php file to do the work of that plugin. Is that possible? If it be a cumbersome job, then I would use this plugin.

As for the Dashboard and admin menu bar, here's the code to make it blank:
PHP:
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>";
});
Thanks for this code. This will be really helpful. Actually, your whole reply is very much helpful. As I mentioned in my first post, I am not a coder. I search Google to solve most of my coding problems but sometimes even Google fails and there, I am at peace that I have an excellent place named Babiato Forum where I have friends like you and @slvrsteele . Who needs Google now? 😉
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
I only now saw that you tried to install a mu-plugin, but that it was unsuccessful. Why didn't that work? It's very simple to do and the code I shared above is from a mu-plugin I use in almost every site I deliver.
You know how to do that? Create a directory under 'wp-content' called 'mu-plugins', put a file in that directory and name it anything you want, mine is called - surprise, surprise - functions.php

And no, in your own functions.php you do NOT copy content from the theme, you only insert code in order to CHANGE things.

If you don't use plug-ins, you should always remove them, even when the theme keeps requesting to install them. What theme is that by the way? Because most themes will stop nagging when you click on "Dismiss this notice" or something similar. But are you sure those plug-ins are not needed for the booking features?

Since you're not a coder I strongly advice to use a plug-in to hide menu items.
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
I only now saw that you tried to install a mu-plugin, but that it was unsuccessful. Why didn't that work? It's very simple to do and the code I shared above is from a mu-plugin I use in almost every site I deliver.
You know how to do that? Create a directory under 'wp-content' called 'mu-plugins', put a file in that directory and name it anything you want, mine is called - surprise, surprise - functions.php

Thanks friend. I was wrongly creating the 'mu-plugins'. The name of your file is 'surprise-functions.php'?! :eek:

And no, in your own functions.php you do NOT copy content from the theme, you only insert code in order to CHANGE things.
Okay. Great!

If you don't use plug-ins, you should always remove them, even when the theme keeps requesting to install them. What theme is that by the way? Because most themes will stop nagging when you click on "Dismiss this notice" or something similar. But are you sure those plug-ins are not needed for the booking features?
The theme name is Hoteller v5.1.1. I have got the latest update which is v5.3 also but haven't updated the theme yet. Right now I am checking the files for any Base64 or suspicious code or links. If all be good, then I will update it. I have done as you told and now all are good. I have uninstalled those 3 plugins and it works fine because I use solely the Hotel Booking Payment System instead of WooCommerce. This Hotel Booking plugin is actually MotoPress Hotel Booking plugin and it is more than sufficient for a single hotel booking site. May be WooCommerce is required in case of multi-hotel setup or partnership with other tour company like Goibibo, MakeMyTrip etc. In my case, these plugins are not needed.

Since you're not a coder I strongly advice to use a plug-in to hide menu items.
Let me try this 'mu-plugins' thing. If it gives me any problem, then I will go for the plugin. But, I have to try first and I will get back to you with the result. Thanks for your awesome help friend. ❤☺
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
No, my plug-in is called 'functions.php' but it really doesn't matter what name you give it. By the way, that code I gave is only for the Dashboard clean-up, but let me know if that worked.
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
I have got the latest update which is v5.3 also but haven't updated the theme yet. Right now I am checking the files for any Base64 or suspicious code or links.
If you got the theme from a resource link here on Babiato, you don't have to check. The only time I'm careful is when some not so well-known member shares a link in a post, but otherwise I trust the resources here.
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
@frizzel :

Super Admin account screenshot:

restrict-access.png

Here, Red Coloured sections which I want to hide and restrict user access. The Green Coloured sections I want to replace with my own logo, 'Welcome' message and may be a version number 😋.

And after I use the code you gave me, this is the screenshot of the custom user account:

after-code.png

And this is the full code of that 'mu-plugin' file:

PHP:
<?php

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




function control()
{

// Checking whether the logged user is an Admin or not
    $user_id = get_current_user_id();



        if($user_id !== 1){
        // If the User ID is !== 1 which means the user is not the Super Admin, then do this:    

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>";
});
}

add_action('init', 'control');
}

Now, please guide me on what to change in this code to achieve my desired results.
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
If you got the theme from a resource link here on Babiato, you don't have to check. The only time I'm careful is when some not so well-known member shares a link in a post, but otherwise I trust the resources here.

No, I got this from an external source but v.5.1.1, as per my checking, it seems to be clean. That's why I asked in this thread about uploading resources beforehand: https://babiato.tech/threads/can-anyone-enlighten-me.41208/ and I decided to help in any other way I can. I also don't check the resources from this forum as I trust the checking rules here and another cause is that, it is also not possible because checking only 1 theme entirely, takes 2 to 3 days. So checking every resources is out of the question. But I have some other forums and sites, from where I am downloading for a long time and they never disappointed me. But, rules are rules. I don't want to break it and put me at a risk to be banned.
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
You should post a screenshot of the dashboard for other admin(s), NOT the Super Admin as the code is only working for admins with another ID than '1'.

And why do you wrap everything in a 'function control'? Do you want help or not? If so, don't try to be more clever and just copy the code AS IS!
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
Here is another Administrator account dashboard:

other-admin-dashboard.jpg

Actually, that previous 'jm' account (the purple coloured dashboard) is assigned to 'Admin' user role which I have created and the above screenshot is of another user's dashboard which has been assigned to the default 'Administrator' user role.
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
And why do you wrap everything in a 'function control'? Do you want help or not? If so, don't try to be more clever and just copy the code AS IS!

I don't want to be clever friend. I was just experimenting with some code I found earlier on the net because I always try to learn. I found that code in a YouTube tutorial which was showing how to make a 'mu-plugin' and there the guy put that code in order to check whether the logged in user is an Administrator' or not. But the problem is, this code will work everyone else other than Administrator, which is not desirable. I kept it there to show you the exact code and where it is placed to ask a solution. May be my way of showing is not correct. Sorry about that.

No, my plug-in is called 'functions.php' but it really doesn't matter what name you give it. By the way, that code I gave is only for the Dashboard clean-up, but let me know if that worked.


You see from the image below that it worked. Only the footer portion is there.

other-user-admin-dashboard.jpg

Here is another user's dashboard whose 'uer role' has been set as 'Shop Manager' which was created by the theme:

user-3-shop_manager-dashboard.jpg

So, you can see that 'dashboard clean-up' thingy is working. Now I have to get rid of the other sections I mentioned earlier. By the way, I have removed all those extra code which you asked me to remove. Here is the current code of the 'restrict.php' (the mu-plugin file):

PHP:
<?php

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




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>";
});

See? I am a very good student. 😇
 
Last edited:

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
Thanks. The reason I was so harsh is: I know this code works. If it doesn't work in your situation and you have added your own code(s) it can be very hard to determine why it doesn't work.
Right, the footer. To remove the footer completely, add this code at the end of the code you already have:
PHP:
add_filter( 'admin_footer_text', '__return_empty_string', 99 );
add_filter( 'update_footer',     '__return_empty_string', 99 );
If you want the footer text replaced with your own, use this code instead:
PHP:
function remove_footer_admin () {
return 'Your text here, can include HTML';
}
add_filter('admin_footer_text', 'remove_footer_admin', 99);

function remove_footer_update () {
return 'Your text here, can include HTML';
}
add_filter('update_footer', 'remove_footer_update', 99);

For removing Menu Items, I still recommend to use a plug-in. Much easier to maintain, and you don't have to search for pages, links or strings added by your theme and/or plug-ins.
 
Last edited:
  • Like
Reactions: DarKMaSk

DarKMaSk

Active member
Nov 17, 2020
306
78
43
Thanks. The reason I was so harsh is: I know this code works. If it doesn't work in your situation and you have added your own code(s) it can be very hard to determine why it doesn't work.

I understand friend. At least that common sense I still have. :D

Right, the footer. To remove the footer completely, add this code at the end of the code you already have:
PHP:
add_filter( 'admin_footer_text', '__return_empty_string', 99 );
add_filter( 'update_footer',     '__return_empty_string', 99 );
If you want the footer text replaced with your own, use this code:
PHP:
function remove_footer_admin () {
return 'Your text here, can include HTML';
}
add_filter('admin_footer_text', 'remove_footer_admin', 99);

function remove_footer_update () {
return 'Your text here, can include HTML';
}
add_filter('update_footer', 'remove_footer_update', 99);

For removing Menu Items, I still recommend to use a plug-in. Much easier to maintain, and you don't have to search for pages, links or strings added by your theme and/or plug-ins.

Thank you very much. I better use that plugin you mentioned for removing Menu Items. I will get back to you with feedback. ☺
 

guguk

Well-known member
Jul 19, 2019
1,150
828
113
Ottoman Empire
Just use Admin Menu Editor Pro and their addons, trust me.
I was doing this job via code before but it's too hard to keep and check if work new WP versions. So i decided to use a plugin and AME is lightweight and useful.
 
  • Like
Reactions: DarKMaSk

DarKMaSk

Active member
Nov 17, 2020
306
78
43
Just use Admin Menu Editor Pro and their addons, trust me.
I was doing this job via code before but it's too hard to keep and check if work new WP versions. So i decided to use a plugin and AME is lightweight and useful.
Thank you for sharing your experience. :) @frizzel also told me about it and gave me the link of that plugin here at the very beginning and now I have two friends with the same opinion which gave me extra confidence. 😌
 

DarKMaSk

Active member
Nov 17, 2020
306
78
43
@frizzel Friend, can you please guide me on how to replace the WordPress logo on the Admin Header bar with my own? And if I need a guidance on how to customize the Admin login page with my own logo, colour and text, will I have to make a separate thread or you can guide me here?
 

frizzel

Well-known member
Trusted Uploader
Jun 13, 2019
485
253
63
Wherever my imagination takes me
The WordPress logo is a so-called 'node' which includes the WordPress menu that I'm quite sure you don't want to have. So, you still need to remove the node 'wp-logo' (but that's already in your code).

To add your own logo in the admin bar add this code:

PHP:
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 );

And then add a little css to the '<style>' code at the end of the original code I supplied, like this:
CSS:
#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;
}
You may need to change the above a bit, depending on your logo size.
 
  • Like
Reactions: DarKMaSk

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