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

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?