X

Add Search Form to Specific wp_nav_menu

Adding this snippet to the functions.php of your wordpress theme 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.

Do you want to customize the navigation menu and display a search form with it? Simply 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;
}

We hope this code helped you add search form to specific wp_nav_menu. You may also want to check out our guide on limit amount of menu items in WordPress.

Comments  Leave a Reply

  1. 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!