Using WordPress ‘is_admin_bar_showing()’ PHP function

The is_admin_bar_showing() WordPress PHP function determines whether the admin bar should be showing.

Usage

if (is_admin_bar_showing()) {
    // do something
}

Parameters

  • None

More information

See WordPress Developer Resources: is_admin_bar_showing()

Examples

Hide a particular element when admin bar is showing

// Hide the site logo when admin bar is showing
if (is_admin_bar_showing()) {
    echo '<style>.site-logo { display: none; }</style>';
}

Add custom CSS class to body when admin bar is showing

// Add 'admin-bar-active' CSS class to the body tag
function add_admin_bar_class($classes) {
    if (is_admin_bar_showing()) {
        $classes[] = 'admin-bar-active';
    }
    return $classes;
}
add_filter('body_class', 'add_admin_bar_class');

Display a message when admin bar is showing

// Show a custom message if the admin bar is active
function show_admin_bar_message() {
    if (is_admin_bar_showing()) {
        echo '<p>The admin bar is currently showing.</p>';
    }
}
add_action('wp_footer', 'show_admin_bar_message');

Adjust the top padding of a fixed header when admin bar is showing

// Add extra top padding to fixed header when admin bar is showing
function adjust_fixed_header_padding() {
    if (is_admin_bar_showing()) {
        echo '<style>.fixed-header { padding-top: 32px; }</style>';
    }
}
add_action('wp_head', 'adjust_fixed_header_padding');

Remove a specific admin bar menu item when admin bar is showing

// Remove the 'Dashboard' menu item from the admin bar
function remove_dashboard_from_admin_bar($wp_admin_bar) {
    if (is_admin_bar_showing()) {
        $wp_admin_bar->remove_menu('dashboard');
    }
}
add_action('admin_bar_menu', 'remove_dashboard_from_admin_bar', 999);