Many times you need to add a search form to a WordPress menu bar. Most of the modern/premium themes already have a search option to be added at the menu bar, and top bar. You can enable or disable this option from your theme options panel. But many free themes don’t provide this option. In that case, we need to add the plugin or customize the theme to achieve the goal.
In this post, I will show you how to add a search form to a WordPress menu.
Add the following code snippet to your theme functions.php
add_filter('wp_nav_menu_items', 'add_search_form_to_menu', 10, 2);
function add_search_form_to_menu($items, $args) {
if( $args->theme_location == '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;
}
Don’t forget to change the menu name in the above code snippet.
You can also add it to a WordPress menu by using Add Search to Menu Plugin.
To summarize, inserting a search box in your WordPress menu not only improves the user experience but also improves the visitors’ ability and access to locate the desired content. Following the steps specified in this article, you can add a search form to your website navigation without any glitches. This minor yet beneficial enhancement can better connect your audience to the site and facilitate quick interaction with its content. Don’t hesitate to make changes to the look and feel of the search form following your theme, and make your website stylish and straightforward at the same time!
If you need any help, don’t hesitate to contact us.