So you’re building a website and want to create the best experience for the visitor. What doesn’t take par in your vision is the standard and out of context WordPress admin bar and dashboard (/wp-admin
). You want this gone and forgotten about for your logged in users as quickly and cleanly as possible.
There are two quick and easy code snippet that will allow you to control if the admin bar should show for the user and if a user should be redirected when trying to access the dashboard.
Removing the Admin Bar for Non-Admins
Removing the admin bar is a simple code snippet. I’ve shown before how you can hide the admin bar conditionally for users. In there I’ve not mentioned how you can hide it for everyone but admins, so thats where this code snippet comes in;
Block Dashboard Access for Non-Admins
The best way to handle someone trying to access the dashboard is to redirect them to another page. You can consider redirecting them back to the primary landing page of your site, their front-end profile (if there’s any) or just redirect them back to the page they came from.
You can change the home_url()
to something else if you want to redirect them somewhere else. For example, to redirect them to the shop page you can use home_url( '/shop' )
or if you have a profile page home_url( '/profile' )
.
Redirecting users after login
A quick sample to redirect user after they’ve logged into your site;
Redirecting users after logout
Same as the above, you can also use it for the logout redirect;
18 thoughts on “How to Block Dashboard Access for Non-Admins”
Nice posts, is there a way to use the Block Dashboard Access for Non-Admins for more user roles like shop shop manager and editor?
Hi Philip,
Yes thats possible to do, you just have to modify the script a bit to change the user role slug(s) and check for multiple roles instead of one..
Cheers,
Jeroen
Just thanks. Very useful for not using plugin and working perfect!
Thanks again
Hi,
is it possible to restrict special tabs in backend (like wc reports)?
for example:
should be ALLOWED
https://demo.com/wp-admin/admin.php?page=wc-reports&tab=orders
should be NOT allowed
https://demo.com/wp-admin/admin.php?page=wc-reports&tab=customers&report=customer_list
Thanks & Regards
Dieter
Hi Dieter,
(Almost) anything is possible, though this post is not focussed on blocking access to specific parts of the admin, but rather the entire admin area.
A further in-depth customization is required to blocking access to specific pages for certain user (roles).
Hi Jeroen,
Nice solution! I’m looking for the same option:
“Nice posts, is there a way to use the Block Dashboard Access for Non-Admins for more user roles like shop shop manager and editor?”
Your answer: Yes thats possible to do, you just have to modify the script a bit to change the user role slug(s) and check for multiple roles instead of one..
I’d like to have the Dashboard acces blocked for everyone except: (Super) Admins and Editors. What extra line should I add to your script for that?
Thanks in advance!
Best regards,
Steven
Hi,
This is a nice post. I am assuming the above code will go into the functions.php file? Might seem like a stupid question but I am new to WordPress.
Regards
David
That is correct.
Thank you!
Hey, thanks for this post. Quick question: How can I block access to wp-admin/index.php ONLY. I’m trying to embed an admin page to a frontend page but trying your snippet is blocking every call. I don’t want people to go to wp-admin/ but they should be able to access admin pages from frontend. Appreciate your help on this!
Hi,
You could consider adding a simple query parameter check to not block the admin for the embed (and pass that query param with the embed URL).
Cheers,
Jeroen
Great snippets, thx!
Re: Block Dashboard Access for Non-Admins
You mentioned, “or just redirect them back to the page they came from.”
How may I do that?
Thx
You can use the
wp_get_referer()
function for that.Thank you very much for block access to WordPress admin page ..
Great info still working in 2021… Thank you!
Thank you, this work for me 🙂
Hi Jeroen…
I found this is the post which originaly posted several years ago and still useful until now.
You help me who are not techie but also i dont want to add more plugin to my WordPress site.