Description
The WordPress Admin Bar is displayed for logged-in users on the frontend by default. This snippet hides the Admin Bar for all users except administrators, providing a cleaner browsing experience while allowing administrators to retain quick access to site management tools.
Steps
- Open your active theme’s
functions.phpfile or a custom functionality plugin. - Add the code below.
- Save the file.
- Log in as a non-administrator to verify the Admin Bar is hidden on the frontend.
/**
* Hide the Admin Bar for non-administrators.
*/
function wpfolks_hide_admin_bar_for_users() {
if ( ! current_user_can( 'administrator' ) && ! is_admin() ) {
show_admin_bar( false );
}
}
add_action( 'after_setup_theme', 'wpfolks_hide_admin_bar_for_users' );