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.

I'm developing a plugin and I want to insert menu items programmatically.

When my theme creates a menu, it uses this call:

wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );

Which resulted in something like this in the HTML:

<div class="nav-menu"><ul>
     <li class="current_page_item"><a href="somewhere">menu-item-1</a></li>
     <li class="page_item page-item-107"><a href="somewhere-else">menu item 2</a></li>
</ul></div>

I want to be able to intercept this somehow and insert my own HTML programmatically before the closing </ul>. How do I hook it up?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Before being printed all the menu items get run through a filter. You can target the new_nav_menu_items filter to tack things on to the menu:

// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
    $homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
    // add the home link to the end of the menu
    $items = $items . $homelink;
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

Or to be more specific, you can target only the desired menu by replacing the add_filter line from above with the following and replacing $menu->slug with your menu's actual slug name:

add_filter( 'wp_nav_menu_{$menu->slug}_items', 'new_nav_menu_items' );

Source Tutorial

share|improve this answer
    
Thanks, but nothing happens. I even tried to put your code verbatim just before my theme's wp_nav_menu call, and still no effect. –  agro1986 Nov 5 '13 at 16:31
    
What do you mean nothing happens? I just double-checked and a Home link is added as the first item in the menu. You must have a menu defined for the menu location and not be falling back to wp_page_menu(). The code definitely works. Whether it answers your question or not is another matter because I couldn't really understand what you were asking. –  helgatheviking Nov 5 '13 at 16:48
    
I've edited the code to add the home link to the end of the menu, but the WordPress principle is still the same. –  helgatheviking Nov 5 '13 at 16:50
    
It didn't work because I didn't have a user defined menu. After I created my own menu, it works. Thanks –  agro1986 Nov 5 '13 at 18:00
    
If you were asking how to add links to your menu, and this answered it, then you should accept this as the answer. –  helgatheviking Nov 5 '13 at 21:08

You can set container to false and use the items_wrap argument to omit the ul, then the function will only output the li tags, letting you wrap it in whatever you need.

wp_nav_menu(
    array(
        'theme_location' => 'primary',
        'items_wrap' => '%3$s',
        'container' => false
    )
);
share|improve this answer
    
Thanks, but it only changes <div class="nav-menu">...</div> to <div class="menu">...</div> So I still couldn't put my contents because wordpress already spits the end of the menu –  agro1986 Nov 5 '13 at 16:29
    
you need to create a menu and assign it to your theme location, I'll guess you haven't done that. tested and working with latest WordPress and TwentyThirteen theme. –  Milo Nov 5 '13 at 16:40
    
I'm using wordpress's theme (twentytwelve) and what I'm trying to do is to change the theme's top menu. So the menu appears in the final HTML with links to my pages as its menu items, but I need to put my own menu item programatically. –  agro1986 Nov 5 '13 at 16:46
    
did you create a menu under Appearance > Menus and assign it to that theme location? –  Milo Nov 5 '13 at 16:52
    
OK, now it works after I created a user-defined menu. Thanks a lot! –  agro1986 Nov 5 '13 at 17:59

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.