Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Thanks! I hadn't thought about it requiring an id like that. And thanks for the heads-up about add_node. –  Brett Shumaker Apr 9 '13 at 14:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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