Home / functions.php / Add search form to specific wp_nav_menu
  • How to 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.

    ( click code to copy )

    wordpress snippet : PHP

    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;

    ( WordPress codex functions, hooks, in this snippet. )

    home, add_filter, wp_nav_menu, esc_attr, __, _n, esc_attr__, home_url, wp,
  • Steven Held

    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?