So, I ran into an interesting issue while adding some admin menu bar links today. When adding links to a parent menu using something like:
// Add the parent menu
$wp_admin_bar->add_menu( array(
'title' => 'Testing MENU',
'href' => false,
'id' => 'parent_id'
));
// Add the child menu
$wp_admin_bar->add_menu( array(
‘title’ => ‘My Child Label’,
‘href’ => ‘#’,
‘parent’ => ‘parent_id’
));
The above code works great and as expected. But what if I wanted, for whatever reason, to add a second child menu item like this:
// Add another child menu
$wp_admin_bar->add_menu( array(
'title' => 'My Child Label *',
'href' => '#',
'parent' => 'parent_id'
));
This will not work. The last menu item overrides the first. I understand that you would want to prevent duplicate titles in an admin menu, but this is technically NOT a duplicate title. To get it to work, I must add a non-special character (a-z,A-Z,0-9) to the end like this:
// Add another child menu
$wp_admin_bar->add_menu( array(
'title' => 'My Child Label *1',
'href' => '#',
'parent' => 'parent_id'
));
It appears that the logic checking for duplicate labels is stripping any non-alphanumeric characters from the end of the title and then comparing that. Does anyone have any insight as to why this is? I can’t think of any reason to not take the entire title into consideration.
Any thoughts?
You are not declaring an ‘id’ key. Without one, the id will default to a sanitized version of the ‘title’ value – hence your overwrite.
Declare a unique id element, and you should be golden.
That said, add_node is now the preferred method of achieving this.