Search Box in Main Nav bar (WordPress)

Hi
I am building a WP site. It has multiple menus. I want a search box to appear in the main (primary) menu only.
I am using this code:

<?php add_filter('wp_nav_menu_items','add_search_box', 10, 2);


function add_search_box($items, $args) {

        ob_start();
        get_search_form();
        $searchform = ob_get_contents();
        ob_end_clean();

    $items .= '<li class="searchbox">' . $searchform . '</li>';
  return $items;


}?>

but it is putting a searchbox into my main menu and sidebar menus.

Anyone know a way to target the main menu only?

Thanks

I found this site, which may help you.

It states to use

$args->theme_location == 'primary'

So you would end up with

<?php add_filter('wp_nav_menu_items','add_search_box', 10, 2); 
  
   
function add_search_box($items, $args) { 
  if ($args->theme_location == 'primary') {
        ob_start(); 
        get_search_form(); 
        $searchform = ob_get_contents(); 
        ob_end_clean(); 
  
    $items .= '<li class="searchbox">' . $searchform . '</li>'; 
  }
  return $items;    
       
}?>

Thanks I tried that but the search box disappeared altogether. I had actually tried adding that line before but assumed I must have done it wrong, so just copied and pasted yours, but no luck.

This:

<?php wp_nav_menu( array(‘menu’ => ‘Project Nav’ )); ?>

seems to be the code for targeting a specific menu, but I’m not really a PHP expert and can’t work out where to put it!

Can you change it to this and give me the additional output it produces?

<?php add_filter('wp_nav_menu_items','add_search_box', 10, 2); 
  
   
function add_search_box($items, $args) { 
  var_dump($args); // I want the output of this, as that should help identify what we are doing wrong :)
  if ($args->theme_location == 'primary') {
        ob_start(); 
        get_search_form(); 
        $searchform = ob_get_contents(); 
        ob_end_clean(); 
  
    $items .= '<li class="searchbox">' . $searchform . '</li>'; 
  }
  return $items;    
       
}?>

Yes, here you go:

object(stdClass)#274 (16) { ["menu"]=> string(0) "" ["container"]=> bool(false) ["container_class"]=> string(0) "" ["container_id"]=> string(0) "" ["menu_class"]=> string(4) "menu" ["menu_id"]=> string(3) "nav" ["echo"]=> bool(true) ["fallback_cb"]=> string(12) "wp_page_menu" ["before"]=> string(0) "" ["after"]=> string(0) "" ["link_before"]=> string(0) "" ["link_after"]=> string(0) "" ["items_wrap"]=> string(36) "

    %3$s

" ["depth"]=> int(3) ["walker"]=> string(0) "" ["theme_location"]=> string(15) "main_navigation" }

Thanks for your help

Okay, try this code

<?php add_filter('wp_nav_menu_items','add_search_box', 10, 2); 
  
   
function add_search_box($items, $args) { 
  if ($args->theme_location == 'main_navigation') {
        ob_start(); 
        get_search_form(); 
        $searchform = ob_get_contents(); 
        ob_end_clean(); 
  
    $items .= '<li class="searchbox">' . $searchform . '</li>'; 
  }
  return $items;    
       
}?>

Fantastic - works perfectly. Thank you so much.
So ‘main_navigation’ instead of ‘primary’

Yes, to clarify a bit, and explain why I used main_navigation, the output of $args showed the theme_location as ‘main_navigation’ not ‘primary’, so I just updated primary to be main_navigation :slight_smile:

Glad it worked.