X

Add Search Form to Specific wp_nav_menu

Snippets by IsItWP

Mostly a search form is a part of the top website header. You can customize the navigation menu and display a search form with it.

Instructions: Add the following code to the functions.php file of your WordPress theme to add search form to specific wp_nav_menu. If you’ve multiple menus, then replace MENU-NAME with the name of the menu where you want to add the search form.

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

You may also enjoy limiting amount of menu items in WordPress.

Comments   Leave a Reply

  1. Hi,

    there is a way to avoid close menu after first click in the input? is happening in my case.

    thank you

    1. You may want check to see if it is an issue with the theme.

  2. I’m trying to figure out how to order the search form within a nav menu. For example a standard menu with Home About Services Contact Us – could I put the search form after Services and before the Contact Us? Or after Home and before About?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!