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.

Thanks to some help on here, I've managed to add a custom search box to my main menu... by adding this to my theme's functions.php

add_filter('wp_nav_menu_items','search_box_function');
  function search_box_function ($nav){
  return $nav."<li class='menu-header-search'><form action='http://example.com/' id='searchform' method='get'><input type='text' name='s' id='s' placeholder='Search'></form></li>";
}

However, I've now added another menu to put in the footer, but the search box gets added to this one too. How would I add the search box to the primary menu only?

My code for registering the menus is:

register_nav_menus( array(
  'primary' => __( 'Primary Navigation', 'twentyten' ),
  'secondary'=>__('Secondary Menu', 'twentyten' ),

 ) );

..and the code to display the secondary menu is:

wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'secondary' ) ); 
share|improve this question
add comment

2 Answers

up vote 21 down vote accepted

To only add the custom search box to the main menu you could pass the second parameter provided by the wp_nav_menu_items filter and check if the theme_location is the primary location

add_filter('wp_nav_menu_items','search_box_function', 10, 2);
function search_box_function( $nav, $args ) {
    if( $args->theme_location == 'primary' )
        return $nav."<li class='menu-header-search'><form action='http://example.com/' id='searchform' method='get'><input type='text' name='s' id='s' placeholder='Search'></form></li>";

    return $nav;
}
share|improve this answer
1  
thanks, that's great... just a newbie question - what does the 10, 2 do in the code? –  cannyboy Sep 23 '10 at 14:45
4  
10 is priority (ten is default), 2 is number of arguments that function we are hooking to filter accepts. –  Rarst Sep 23 '10 at 15:25
    
@ ampt, Yes, I tried that. It worked. almost. When I do login, my second menu links dissapear, how can I solve that? –  user9182 Oct 4 '11 at 23:59
    
Note that as of WP 3.8, the variable is $args->menu instead of $args->theme_location –  Alain Jacomet Forte Mar 26 at 21:00
add comment

An alternative method of doing this is adding the menu slug to the wp_nav_menu_items filter.

For example, let's say you have a menu named Header and you always want this menu (whether it's attached to a theme location or not) to display a search box. You can do so by adding the menu slug, in this case header, to the filter.

The new filter would be as follows:

add_filter( 'wp_nav_menu_header_items', 'search_box_function' );

Notice the header portion of the new filter. This tell WordPress what menu to add the function to.

This is just one different way to approach your current problem.

share|improve this answer
    
See the item from @oshi on this page, which is addressed to you. –  halfer Jan 2 '13 at 1:39
add comment

protected by Community Mar 1 '13 at 20:46

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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