Add Snippet To Project
If you use WordPress regularly, you’re probably familiar with the admin bar that appears at the top of your screen when you’re logged in. Did you know that you can customize this bar to include a navigation menu? By doing so, you can make it easier to access important pages on your site while you’re logged in. In this article, we’ll guide you through the steps to attach a navigation menu to the admin bar in WordPress. Let’s get started!
function wpturbo_add_admin_bar_menu() {
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() ) {
return;
}
$wp_admin_bar->add_menu( array(
'id' => 'wpturbo-admin-menu',
'title' => __( 'My Navigation Menu', 'wpturbo' ),
'href' => '#',
) );
$menu_items = wp_get_nav_menu_items( 'my-navigation-menu' );
if ( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
$wp_admin_bar->add_menu( array(
'parent' => 'wpturbo-admin-menu',
'id' => 'wpturbo-admin-menu-' . $menu_item->ID,
'title' => $menu_item->title,
'href' => $menu_item->url,
) );
}
}
}
add_action( 'admin_bar_menu', 'wpturbo_add_admin_bar_menu', 999 );
In this code snippet, we define a new function called wpturbo_add_admin_bar_menu()
, which will allow us to attach a custom navigation menu to the WordPress admin bar.
We start by making sure that the current user is a super admin and that the admin bar is showing. We use the is_super_admin()
function to check whether the current user has the permissions required to access the network admin screens, and is_admin_bar_showing()
to determine whether the admin bar is being displayed.
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() ) {
return;
}
Next, we use the $wp_admin_bar->add_menu()
method to add a new top-level menu item to the admin bar. We specify the ID and title of the menu, as well as a placeholder URL.
$wp_admin_bar->add_menu( array(
'id' => 'wpturbo-admin-menu',
'title' => __( 'My Navigation Menu', 'wpturbo' ),
'href' => '#',
) );
We then use the wp_get_nav_menu_items()
function to retrieve the menu items from our custom navigation menu. We specify the name of the menu ("my-navigation-menu" in this example), which will need to be replaced with the name of your own custom menu.
$menu_items = wp_get_nav_menu_items( 'my-navigation-menu' );
If the menu items exist, we loop through them using a foreach
loop and add each item as a child menu item to our top-level menu. We use the $wp_admin_bar->add_menu()
method again to add the child menu items, specifying the parent ID, the ID and title of the menu item, and the URL.
if ( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
$wp_admin_bar->add_menu( array(
'parent' => 'wpturbo-admin-menu',
'id' => 'wpturbo-admin-menu-' . $menu_item->ID,
'title' => $menu_item->title,
'href' => $menu_item->url,
) );
}
}
Finally, we hook our wpturbo_add_admin_bar_menu()
function into the admin_bar_menu
action, with a priority of 999. This ensures that our menu is added after other items have been added to the admin bar.
add_action( 'admin_bar_menu', 'wpturbo_add_admin_bar_menu', 999 );
And that’s it! With this code, you should be able to attach a custom navigation menu to your WordPress admin bar.