in WordPress

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;
}

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email