How to Only Show the Admin Bar to Administrators in WordPress

Home » Snippets » How to Only Show the Admin Bar to Administrators in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

WordPress comes with an admin bar displayed at the top of the screen for logged-in users. While this feature can be useful for quick access to various administrative tasks, it’s not always necessary for all types of users to have it displayed. If you’re managing a WordPress site with multiple users, you might want to limit the admin bar visibility to just administrators. In this article, we’ll show you how to do just that.

					function wpturbo_admin_bar() {
    if ( ! current_user_can( 'manage_options' ) ) {
        add_filter( 'show_admin_bar', '__return_false' );
    }
}
add_action( 'after_setup_theme', 'wpturbo_admin_bar' );
				

The wpturbo_admin_bar() function is used in the code snippet to control whether or not to show the WordPress admin bar. The if statement checks the user capabilities of the current user and determines whether they should see the admin bar or not.

If the current user does not have the manage_options capability, which is usually assigned to administrators, then the show_admin_bar filter is set to false. This effectively hides the admin bar for non-admin users (e.g., subscribers).

The add_filter() function is used to modify the show_admin_bar setting, and it takes two parameters: the filter name and the callback function. In this case, we return a value of false which hides the admin bar for non-administrators.

Finally, the add_action() function is used to hook the wpturbo_admin_bar() function into the after_setup_theme action. This ensures that the function is executed as early as possible during the WordPress load process, making sure that any changes made to the admin bar display are applied correctly.

By adding this code snippet to your functions.php file, your WordPress site will only show the admin bar to users who have the manage_options capability.

Register an account to save your snippets or go Pro to get more features.