In WordPress, if you don’t want non-admins to even be able to access the wp-admin dashboard than here is solutions. This code 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.
Here i show you different case. Use it as per your requirement. Add this code in your theme’s functions.php file of your active child theme (or theme) and save the file.
Note : 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’ ).
Removing the Admin Bar for Non-Admins
function hide_admin_bar( $show ) {
if ( ! current_user_can( 'administrator' ) ) {
return false;
}
return $show;
}
add_filter( 'show_admin_bar', 'hide_admin_bar' );
Block Dashboard Access for Non-Admins
function block_wp_admin() {
if ( is_admin() && ! current_user_can( 'administrator' ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_safe_redirect( home_url('/shop') );
exit;
}
}
add_action( 'admin_init', 'block_wp_admin' );
Redirecting users after login
function login_redirect( $url ) {
return home_url( '/profile' );
}
add_filter( 'login_redirect', 'login_redirect' );
Redirecting users after logout
function logout_redirect( $url ) {
return home_url( '/profile' );
}
add_filter( 'logout_redirect', 'logout_redirect' );