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

2 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

Leave a Reply