Add Search Form To A WordPress Menu

Wordpress

Wordpress comes with the functionality to create menus in the dashboard area, from here you can select pages or custom custom menu items to make up your menu.

Menus are normally going to be display at the top of the page to give the visitor easy access to your main pages. But this top navigation bar is also a good place to have your search box so if people can't find what they are looking for then they can use your search box.

With the default behaviour of the WordPress menus you can only add pages to the menu and not a search box.

Below is a WordPress snippet you can use to add a search form to a certain menu, just add the following to your WordPress functions.php file.

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

 function add_search_form($items, $args) {
          if( $args->theme_location == 'CHANGE-THIS-TO-YOUR-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="'. __('Search') .'" /></form></li>';
     return $items;
}

Advertise here

Comment