How remove admin bar and block access for users.
All WordPress users know this black bar at the top of the page after login in. As an admin, we find this useful, but in some cases, we do not want this to be visible for our users. There is a simple trick to hide this.
If you do not know how to use code snippet, please refer to this article with an explanation.
add_action('after_setup_theme', 'remove_admin_bar'); function remove_admin_bar() { if ( !current_user_can( 'manage_options' ) && !is_admin() ) { show_admin_bar(false); } }
Before:
After:
If you want to block access to wp-admin
The black bar is one thing. But more advanced users will still be able to enter wp-admin section, by adding wp-admin to page url. You can prevent this, by redirecting non-admin users to the home page. Just use this snippet:
add_action( 'init', 'wpjb_hide_wp_admin'); function wpjb_hide_wp_admin() { // Check if user is administrator $user = wp_get_current_user(); $allowed_roles = array('administrator'); if( array_intersect( $allowed_roles, $user->roles ) ) { return; } // Check if user is accessing backend, but make exception for ajax actions if( is_admin() && !defined( 'DOING_AJAX' ) ) { wp_redirect( home_url() ); } }