WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some code that adds a menu item to a menu using wp_nav_menu_{$menu->slug}_items.

It's pretty easy to add a menu item as the first or last item of the menu, but I was wondering if there's any way to assign an order to the item, i.e. place it as the 3rd item in the menu.

If there's no method for that built into the filter, is there another way to achieve the same result?

share|improve this question
thanks @s_ha_dum, I forgot to do that... – Jeremiah Prummer May 24 '13 at 21:57

you can always search & replace by string or regex the output html of the filtered $items. anyway, if you're targeting usual menu items, you can add $post objects and order items through the wp_nav_menu_objects filter, otherwise here's the basic sample code:

function replace_itemcart_in_menu( $items, $args ) { 
    $url = home_url('my-item');
    $previtem = '<li id="menu-item-11" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-11"><a href="'.$carturl.'/">My Item</a>'; 
    $newitem = '<li>New Item</li>'; 
    $items = str_replace($previtem, $previtem.$newitem, $items); 
    return $items; 
share|improve this answer

Your Answer


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.