How to add custom menu item in admin bar or in toolbar?

By using hooks & filters of wordpress, we can use add new custom menu item under admin bar. there are no need to use any plugin.

admin_bar_menu hooks is used to add the admin bar menu.

To add new item menu in admin bar, you will require to add given below code into your theme function.php file.

if(!function_exists('add_custom_admin_bar_item')):
add_action( 'admin_bar_menu', 'add_custom_admin_bar_item', 500 );
function add_custom_admin_bar_item($wp_admin_bar) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    $admin_bar->add_menu( array(
        'id'    => 'custom-menu',
        'parent' => null,
        'group'  => null,
        'title' => 'My Menu', //you can use img tag with image link. it will show the image icon Instead of the title.
        'href'  => admin_url('admin.php?page=custom-page'),
        'meta' => [
            'title' => __( 'Menu Menu', 'textdomain' ), //This title will show on hover
        ]
    ) );
}
endif;

To add multi level admin bar menu item or want to add a new menu item under exsting parent admin bar menu item them you will required to define it’s parent menu item id. As per given above code, there are parent menu id is “custom-menu”. If we want to add a sub menu under “custom-menu” then we will need to define it’s parent id to “custom-menu”

if(!function_exists('add_custom_admin_bar_item')):
add_action( 'admin_bar_menu', 'add_custom_admin_bar_item', 500 );
function add_custom_admin_bar_item(WP_Admin_Bar $admin_bar) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    $admin_bar->add_menu( array(
        'id'    => 'custom-menu',
        'parent' => null,
        'group'  => null,
        'title' => 'My Menu', //you can use img tag with image link. it will show the image icon Instead of the title.
        'href'  => admin_url('admin.php?page=custom-page'),
        'meta' => [
            'title' => __( 'Menu Menu', 'textdomain' ), //This title will show on hover
        ]
    ) );
    
    //First lavel
	$admin_bar->add_menu( array(
		'id'    => 'first_lavel',
		'parent' => 'custom-menu',
		'title' => 'First Lavel',
		'href'  => admin_url('themes.php?page=ve-options'),
		'meta'  => array(
			'title' => __('First Level'),
			'target' => '_self',
			'class' => 've_menu_item_class'
		),
	));
	
    //First lavel 1
	$admin_bar->add_menu( array(
		'id'    => 'first_lavel1',
		'parent' => 'custom-menu',
		'title' => 'Demo Lavel',
		'href'  => admin_url('themes.php?page=ve-options'),
		'meta'  => array(
			'title' => __('Demo Level'),
			'target' => '_self',
			'class' => 've_menu_item_class'
		),
	));
	
    //Second lavel
	$admin_bar->add_menu( array(
		'id'    => 'second_lavel',
		'parent' => 'first_lavel',
		'title' => 'Second Lavel',
		'href'  => admin_url('themes.php?page=ve-options'),
		'meta'  => array(
			'title' => __('Second Lavel'),
			'target' => '_self',
			'class' => 've_menu_item_class'
		),
	));
    
}
endif;

All the best!

Leave a Reply

Your email address will not be published. Required fields are marked *