show_admin_barfilter-hookWP 3.1.0

Allows you to show or hide the admin bar (toolbar) on the front end. The hook works only on the front end; it does not work in the admin area.

What the admin bar (toolbar) looks like

There is also the show_admin_bar() function, which gets or sets the global $show_admin_bar variable. That variable is passed through this filter hook.

Therefore, this hook takes precedence over the show_admin_bar() function.

Usage

add_filter( 'show_admin_bar', 'wp_kama_show_admin_bar_filter' );

/**
 * Function for `show_admin_bar` filter-hook.
 * 
 * @param bool $show_admin_bar Whether the admin bar should be shown.
 *
 * @return bool
 */
function wp_kama_show_admin_bar_filter( $show_admin_bar ){

	// filter...
	return $show_admin_bar;
}
$show_admin_bar(true|false)
Whether the admin bar (toolbar) should be displayed.
Default: false

Examples

#1 A simple example of disabling or enabling the admin bar

Disable the admin bar:

// Disable the admin bar.
add_filter( 'show_admin_bar', '__return_false' );

Enable the admin bar:

// Enable the admin bar.
add_filter( 'show_admin_bar', '__return_true' );

#2 Display the admin bar on the front end only for editors

This code accounts for the possibility that $show_admin_bar has already been disabled, in which case no unnecessary checks are performed.

add_filter( 'show_admin_bar', 'admin_bar_for_admin_only', 99 );

function admin_bar_for_admin_only( $show_admin_bar ) {

	if ( $show_admin_bar && ! current_user_can( 'edit_others_posts' ) ) {
		$show_admin_bar = false;
	}

	return $show_admin_bar;
}

#3 How to disable the admin bar in the admin area

The admin bar is very important in the admin area, so this hook affects only the toolbar on the front end. If you nevertheless need to disable the toolbar in the admin area, you can use this hack:

// Disable the admin bar in the admin area.
remove_action( 'in_admin_header', 'wp_admin_bar_render', 0 );

Changelog

Since 3.1.0 Introduced.

Where the hook is called

is_admin_bar_showing()
show_admin_bar
wp-includes/admin-bar.php 1465
$show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );

Where the hook is used in WordPress

Usage not found.