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>

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?


2 Answers 2


Before being printed, all the menu items get run through a filter. You can target the wp_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

  • 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. Commented Nov 5, 2013 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. Commented Nov 5, 2013 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. Commented Nov 5, 2013 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 Commented Nov 5, 2013 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. Commented Nov 5, 2013 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.

        'theme_location' => 'primary',
        'items_wrap' => '%3$s',
        'container' => false
  • 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 Commented Nov 5, 2013 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
    Commented Nov 5, 2013 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. Commented Nov 5, 2013 at 16:46
  • did you create a menu under Appearance > Menus and assign it to that theme location?
    – Milo
    Commented Nov 5, 2013 at 16:52
  • OK, now it works after I created a user-defined menu. Thanks a lot! Commented Nov 5, 2013 at 17:59

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.