How to Hide, Customize, and Add Items to the WordPress Admin Bar

Hide, clean up and extend the WordPress admin bar

The admin bar, the dark toolbar at the top of the site for logged-in users, is handy for administrators but confusing for customers and subscribers, and it can overlap a fixed header. On the other hand, it is a good place for your own quick links. WordPress has a function to hide it and an object to remove and add its items.

Problem: Customers and subscribers see the admin bar with items they do not need, while the administrator would like a few extra shortcuts in it.

Solution: Hide the bar for everybody without manage_options with show_admin_bar( false ), remove default items with $wp_admin_bar->remove_node(), and add your own with $wp_admin_bar->add_node() on the admin_bar_menu hook.

Hide it for non-administrators

<?php
add_action( 'after_setup_theme', function () {
    if ( ! current_user_can( 'manage_options' ) ) {
        show_admin_bar( false );
    }
} );

This hides the bar on the front end only; in the admin area it is always shown. Every user can also switch it off for themselves in their profile (“Show Toolbar when viewing site”).

Remove default items

<?php
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
    $wp_admin_bar->remove_node( 'wp-logo' );     // WordPress logo and its menu
    $wp_admin_bar->remove_node( 'comments' );    // comments counter
    $wp_admin_bar->remove_node( 'new-content' ); // "+ New"
    $wp_admin_bar->remove_node( 'search' );      // search field on the front end
}, 999 );

Priority 999 runs after core has added its items. Do not remove my-account unless you offer another logout link: it contains “Log Out”.

Add your own items

<?php
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
    if ( ! current_user_can( 'edit_posts' ) ) {
        return;
    }

    $wp_admin_bar->add_node( array(
        'id'    => 'ha-shortcuts',
        'title' => 'Shortcuts',
        'href'  => false,
    ) );

    $wp_admin_bar->add_node( array(
        'id'     => 'ha-shortcuts-events',
        'parent' => 'ha-shortcuts',
        'title'  => 'Events',
        'href'   => admin_url( 'edit.php?post_type=event' ),
    ) );

    $wp_admin_bar->add_node( array(
        'id'     => 'ha-shortcuts-customizer',
        'parent' => 'ha-shortcuts',
        'title'  => 'Customizer',
        'href'   => admin_url( 'customize.php' ),
    ) );
}, 100 );

A node with parent becomes a dropdown item of that node. With 'meta' => array( 'target' => '_blank' ) a link opens in a new tab.

The fixed header problem

When the bar is shown, WordPress adds margin-top: 32px to html (46px on small screens) and the class admin-bar to body. A fixed header must move down by the same amount:

.admin-bar .site-header--fixed {
    top: 32px;
}

@media screen and (max-width: 782px) {
    .admin-bar .site-header--fixed {
        top: 46px;
    }
}

NOTE: To find the ID of an item you want to remove, look at the HTML: every item is an li with the ID wp-admin-bar-{id}, for example wp-admin-bar-comments. Hiding the bar is about convenience, not security: users still have the same rights in wp-admin.