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;