How to Add or Remove Links From the WordPress 3.1 Admin Bar

The new WordPress 3.1 admin bar is fairly handy and it can save you quite a bit of time when accessing your most-used menu items. However, not every WordPress installation is the same. We’re going to show you how you can customize the admin bar to suit your particular needs.

How to remove links from the admin bar

Take a look at the default admin bar:

Now, let’s turn something off. Let’s say, for example, that you don’t have comments enabled for your site and therefore do not require easy access to the comments menu item. Here’s how you can remove that from the admin bar. Add this snippet to your theme’s functions.php file:

1
2
3
4
5
6
7
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
    
}

add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

Inspired by code from wp-snippets.com

Now your admin bar should look something like this:

Pretty simple, right? If you take a look at /wp-includes/admin-bar.php, you can find ID’s for other items you’d like to remove.

How to add links to the admin bar

By default the Add New menu item on the admin bar contains Post and Page. In this example, we’re going to add Media to the drop down, as that seems like it might come in handy. Add this snippet to your theme’s functions.php file:

1
2
3
4
5
6
7
8
9
10
11
12
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
        $wp_admin_bar->add_menu( array(
        'parent' => 'new-content',
        'id' => 'new_media',
        'title' => __('Media'),
        'href' => admin_url( 'media-new.php')
    ) );
    
}

add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

You can follow this same pattern for any new item that you’d like to add to the admin bar, replacing each ID depending on what you’d like to add to the menu. Here are the basics we’re working with, as listed on wp-snippets.com:

my-account / my-account-with-avatar : the first link, to your account. Note that the ID here changes depending on if you have Avatars enabled or not.
my-blogs : the ‘My Sites’ menu if the user has more than one site
get-shortlink : provides a Shortlink to that page
edit : link to Edit [content-type]
new-content : the ‘Add New’ dropdown
comments : the ‘Comments’ dropdown
appearance : the ‘Appearance’ dropdown
updates : the ‘Updates’ dropdown

I hope these two examples of removing and adding links to the admin bar will help you get started on your own customizations. There will probably be plugins available for this soon, but why use a plugin when you can keep your site lean and do it yourself?

Comments (18)

  1. Thank you for the tip, this is very useful.

    The next step would be to edit the bar for all blogs in a network. Would that only be possible by:

    1- Changing the core files, so you need to redo the changes after every WP update
    2- Change all the themes functions.php files (can be hundreds), and if you want to change a link, you need to do it all again?
    3- Some better way?

    Thanks!

    ps: the tab-order of this theme is seriously messed up! hard to navigate the comment fields :)

  2. @Harry: check out the link Adam posted, that answers your question.

    @Sarah: I’like to know how I can add a simple link to my admin bar and how can I position it? I’d like ot have it added on the right side of the admin bar

  3. This code is good, but if we look deeper in the WP Admin Bar class, we’ll see that the ‘remove_menu’ is now just a wrapper version of ‘remove_node’, which means WP team now use the term ‘node’ instead of ‘menu.

    And while we can copy this code to functions.php file, it’s better to use a plugin like Hide Admin Menu to reduce the effort. This plugin allow us to select which admin bar items need to be hidden based on user role, just select the check boxes and Save.

Participate