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 9 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.

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