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;

Jeroen Sormani

I'm a professional WordPress plugin developer on a mission to create the best plugins for my clients. I'm specialised in developing general WordPress, WooCommerce and Easy Digital Downloads plugins.

Interested in talking about a WordPress project? Get in touch!

Follow me on Twitter

18 thoughts on “How to Block Dashboard Access for Non-Admins

Philip May 2, 2018 at 11:38 pm

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?

Jeroen Sormani May 3, 2018 at 9:25 am

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

Fernando Medina January 10, 2019 at 8:22 pm

Just thanks. Very useful for not using plugin and working perfect!

Thanks again

DieterSchummer March 9, 2019 at 6:49 am

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

Jeroen Sormani March 20, 2019 at 4:38 pm

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

Steven April 2, 2019 at 2:15 pm

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

David April 9, 2019 at 9:46 am

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

Jeroen Sormani April 9, 2019 at 9:51 am

That is correct.

David April 9, 2019 at 11:48 am

Thank you!

Siddiqi May 29, 2019 at 9:25 pm

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!

Jeroen Sormani May 30, 2019 at 10:36 am

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

Daniel Hartzheim August 6, 2019 at 9:02 pm

Great snippets, thx!

Daniel August 6, 2019 at 10:10 pm

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

Jeroen Sormani August 7, 2019 at 9:20 am

You can use the wp_get_referer() function for that.

Codeaxia Digital Solutions February 13, 2021 at 3:34 pm

Thank you very much for block access to WordPress admin page ..

Jason December 17, 2021 at 7:52 pm

Great info still working in 2021… Thank you!

Febri Tri Harmoko March 9, 2022 at 9:21 pm

Thank you, this work for me 🙂

Toko Daring August 4, 2024 at 7:14 am

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.

Leave a Reply