WordPress Snippets at WPcustoms

Create your own admin bar dropdown menu

Build your own admin bar menu with as many sub items as required.


/**
 * Snippet Name: Create your own admin bar dropdown menu
 * Snippet URL: http://www.wpcustoms.net/snippets/create-your-own-admin-bar-dropdown-menu/
 */
 function wpc_add_toolbar_menu($admin_bar){
    $admin_bar->add_menu( array(
        'id'    => 'my-link',
        'title' => 'My link',
        'href'  => '#',
        'meta'  => array(
            'title' => __('My Link'),
        ),
    ));
    $admin_bar->add_menu( array(
        'id'    => 'my-sub-link',
        'parent' => 'my-link',
        'title' => 'My Sub Menu Link',
        'href'  => '#',
        'meta'  => array(
            'title' => __('My Sub Menu Link'),
            'target' => '_blank',
            'class' => 'my_menu_item_class'
        ),
    ));
    $admin_bar->add_menu( array(
        'id'    => 'my-second-sub-link',
        'parent' => 'my-link',
        'title' => 'My Second Sub Menu Link',
        'href'  => '#',
        'meta'  => array(
            'title' => __('My Second Sub Menu Link'),
            'target' => '_blank',
            'class' => 'my_menu_item_class'
        ),
    ));
}
add_action('admin_bar_menu', 'wpc_add_toolbar_menu', 100);