show_admin_bar( bool $show )

Sets the display status of the admin bar.

Description

This can be called immediately upon plugin load. It does not need to be called from a function hooked to the ‘init’ action.

Parameters

$showboolrequired
Whether to allow the admin bar to show.

More Information

This function should be called immediately upon plugin load or placed in the theme’s functions.php file.

This function will also affect the display of the Toolbar in the dashboard for WordPress versions prior to Version 3.3.

Source

function show_admin_bar( $show ) {
	global $show_admin_bar;
	$show_admin_bar = (bool) $show;
}

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

  1. Skip to note 10 content

    This function can be called immediately from the functions.php or the main plugin file to disable the admin-bar for all users.

    If you want to conditionally hide the admin-bar based on the current page or user role, then you can do it in/before the following actions:

    Front-End

    // To disable admin-bar on front-end, the last
    // possibility is action "wp" at any Prio.
    add_action( 'wp', 'prefix_maybe_hide_admin_bar' );

    wp-admin

    // To disable admin-bar inside wp-admin. the last
    // possibility is action "admin_init" at Prio 9
    add_action( 'admin_init', 'prefix_maybe_hide_admin_bar', 9 );

    Full sample

    add_action( 'wp', 'wpdocs_maybe_hide_admin_bar' );
    add_action( 'admin_init', 'wpdocs_maybe_hide_admin_bar', 9 );
    
    function wpdocs_maybe_hide_admin_bar() {
        if ( ! current_user_can( 'manage_options' ) ) {
            show_admin_bar( false );
        }
    }

    Note:

    The init action is fired before wp and admin_init. It’s possible to use show_admin_bar() inside the init action, but other plugins might overwrite your decision at a later point.

    I do NOT recommend using the filter add_filter( 'show_admin_bar', '__return_false' ); in shared plugins! It will create conflicts with other plugins. Only use the filter in your private (child) theme or internal plugins, and stick with the show_admin_bar() function in public or shared plugins.

  2. Skip to note 12 content

    With this example we can hide the admin bar for all users, but for users with “edit posts” permissions, they can opt-in to showing the bar via the “Show Toolbar when viewing site” option on their profile.

    You can flip the logic so those users can opt-out of showing the bar as well.

    // Display the admin bar for editors and above
    add_filter( 'show_admin_bar', function ( $bool ) {
    	if ( empty( $bool ) ) {
    		return false;
    	}
    
    	if ( current_user_can( 'edit_posts' ) ) {
    		// Note: get_user_option() will return a string (ie. 'false'/'true'/'') and not a bool or null
    	        $show_bar_setting = get_user_option( 'show_admin_bar_front' );
    
            	// Default for these users is to hide the bar but allow them to turn it on
    	        return ( 'true' === $show_bar_setting ? true : false );
    	}
    
    	return false;
    } );

You must log in before being able to contribute a note or feedback.