fbpx
How to Add a WordPress Search Form to a Specific "wp_nav_menu"

How to Add a WordPress Search Form to a Specific “wp_nav_menu”

This snippet will add the search form to your WordPress wp_nav_menu. Don’t forget to update the MENU-NAME to specify the menu you wish to display within, just in case you have multiple menus.

Instructions
Add this code to your functions.php file.

add_filter('wp_nav_menu_items', 'add_search_form', 10, 2);

 function add_search_form($items, $args) {
          if( $args->theme_location == 'MENU-NAME' )
          $items .= '<li class="search"><form role="search" method="get" id="searchform" action="'.home_url( '/' ).'"><input type="text" value="search" name="s" id="s" /><input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /></form></li>';
     return $items;
}

WordPress Snippet Info

Tested with Wordpress v. 4.8
Last Modified: 16.05.2017
Last Reviewed: 16.05.2017
Review Status: Needs Review

How to Add a WordPress Search Form to a Specific “wp_nav_menu”
4.4 (87.5%) 8 vote[s]
Please report broken snippets.
Report Broken Snippet