2

I have a function which adds an extra item (and sub-menu items) to the admin bar.

It shows a debug error of... Notice: WP_Admin_Bar::add_node was called incorrectly. The menu ID should not be empty.

function add_admin_bar_menu_misc() {
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() )
return;

$wp_admin_bar->add_menu( array(
'id' => 'misc',
'title' => 'Misc',
'href' => FALSE ) );

$wp_admin_bar->add_menu( array(
'parent' => 'misc',
'title' => 'Export',
'href' => admin_url('export.php'), ) );

$wp_admin_bar->add_menu( array(
'parent' => 'misc',
'title' => 'Back-up',
'href' => admin_url('admin.php?page=wp-dbmanager%2Fdatabase-    backup.php'), ) );

}
add_action('admin_bar_menu', 'add_admin_bar_menu_misc', 999);

What do I need to do to fix it?

3
  • Just like with 'id' => 'misc', add id-s to other menu items also?
    – D. Dan
    Commented May 14, 2018 at 14:52
  • I have got the same problem... Nobody got a issue?
    – user232630
    Commented May 25, 2023 at 10:09
  • The same problem "WP_Admin_Bar::add_node was called incorrectly. The menu ID should not be empty" ? Can you post a new question and include some code to show us how you're calling it?
    – Rup
    Commented May 25, 2023 at 10:15

1 Answer 1

0

The answer is in the error message:

The menu ID should not be empty.

Notice in the first call there is an id field:

$wp_admin_bar->add_menu( array(
'id' => 'misc',
'title' => 'Misc',
'href' => FALSE ) );

But, in the second and third there is no ID!

$wp_admin_bar->add_menu( array(
'parent' => 'misc',
'title' => 'Export',
'href' => admin_url('export.php'), ) );

$wp_admin_bar->add_menu( array(
'parent' => 'misc',
'title' => 'Back-up',
'href' => admin_url('admin.php?page=wp-dbmanager%2Fdatabase-    backup.php'), ) );

Therefore it is empty, and that is not allowed.

If we look at the official documentation for add_menu we see that id is required and not optional:

enter image description here

https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/

We also see that the user notes on that page link to examples on related hooks, and all of them have an id, e.g.

    $wp_admin_bar->add_menu(
        array(
            'parent' => $menu_id,
            'title'  => __( 'Disable', 'text-domain'  ),
            'id'     => 'new-order-notification-disable',
            'href'   => admin_url( 'admin.php?page=custom-submenu-slug' ),

        )
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.