Problem: I would like to completely restrict backend access from a specific User role.
Solution: Here's a code example showing how to restrict users in a single role from the admin:
add_action( 'admin_init', 'restrict_admin_with_redirect', 1 ); function restrict_admin_with_redirect() { $user = wp_get_current_user(); $roles = $user->roles; $role = array_shift($roles); if ( $role == 'hide-from-role-slug' && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { wp_redirect( site_url() ); exit; } }
Replace hide-from-role-slug with the slug of the role you want to restrict.
Then you'll probably want to hide the admin bar as well. I have an example here:
add_filter('show_admin_bar', 'hide_for_roles_func'); function hide_for_roles_func(){ if( is_user_logged_in() ){ $user = wp_get_current_user(); $roles = $user->roles; $role = array_shift($roles); if( $role == 'hide-from-role-slug' ){ return false; } else { return user_can( $user, 'manage_options'); } } return false; }
Replace 'hide-from-role-slug' with the slug of the role you want to restrict.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 4 replies, has 2 voices.
Last updated by 5 years, 3 months ago.
Assisted by: Christian Cox.