Add a Submenu to the WordPress Admin Bar

What to add functionality to your wordpress admin bar? What to add a sub menu?

Example

The example below show the code that needs adding to functions.php in your custom wordpress theme.

1
2
3
4
5
6
7
8
9
10
function create_php_menu() {
    global $wp_admin_bar;

    $menu_id = 'php';
    $wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('phptricks'), 'href' => '/'));
    $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Homepage'), 'id' => 'php-home', 'href' => '/', 'meta' => array('target' => '_blank')));
    $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Drafts'), 'id' => 'php-drafts', 'href' => 'edit.php?post_status=draft&post_type=post'));
    $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Pending Comments'), 'id' => 'php-pending', 'href' => 'edit-comments.php?comment_status=moderated'));
}
add_action('admin_bar_menu', 'create_php_menu', 2000);

All code is open source and everyone if free to use.