Today I will show you how you can redirect certain user role to homepage, or restrict certain user level from accessing the admin dashboard area in WordPress. Say you create a site where you have different user levels that can access certain part of your site (front end) using their login credentials. However you do not want them to get inside the Dashboard (even though the access is restricted). For that all you have to do is add the following snippet to your theme file’s functions.php
function wp_cp_redirect(){
if( is_admin() && !defined('DOING_AJAX') && ( current_user_can('subscriber') || current_user_can('contributor') ) ){
wp_redirect(home_url());
exit;
}
}
add_action('init','wp_cp_redirect');
What this does is redirects any user whose role is either ‘subscriber’ or ‘contributor’ back to the homepage every time they try to access the admin dashboard.
Also a bonus tip (I am feeling happy today ;) )
If you do not want to show the Admin Menu on top of the page once you login, you could use the following code to do so. Just paste it into your theme’s function.php
add_filter( 'show_admin_bar', '__return_false' );