How to Include a Search Form in Your WordPress Navigation Menu

Home » Snippets » How to Include a Search Form in Your WordPress Navigation Menu
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Do you want to add a search form to your WordPress navigation menu? This feature is extremely useful as it improves the functionality of your website by making it easier for visitors to more directly find the content they’re interested in. However, it may not be immediately clear how to achieve this feature. So, if you are looking for an easy and efficient way to include a search form in your WP (WordPress) navigation menu, you’re in the right place. This detailed guide will walk you through the step-by-step process.

					class WPTurbo_Custom_Nav_Walker extends Walker_Nav_Menu {

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        if( 'search' == $item->title ) {
            $output .= get_search_form(false);
        } else {
            parent::start_el( $output, $item, $depth, $args, $id );
        }
    }
}

function wpturbo_add_search_form_to_nav_menu( $items, $args ) {
    if( $args->theme_location == 'primary' ) {
        $search_item = array( 
            'title' => 'search', 
            'menu_item_parent' => 0, 
            'ID' => 'searchform', 
            'db_id' => '', 
            'url' => '#', 
            'xfn' => '', 
            'object' => '', 
            'object_id' => '', 
            'classes' => array(), 
            'type' => '', 
            'status' => '', 
            'description' => '', 
            'attr_title' => '', 
            'target' => '', 
            'current' => ''
        );
        $items[] = (object)$search_item;
    }
    return $items;
}

add_filter( 'wp_nav_menu_objects', 'wpturbo_add_search_form_to_nav_menu', 10, 2 );

function wpturbo_use_custom_nav_walker( $args ) {
    $args['walker'] = new WPTurbo_Custom_Nav_Walker();
    return $args;
}

add_filter( 'wp_nav_menu_args', 'wpturbo_use_custom_nav_walker' );
				

At the start of the snippet, a custom navigation walker class, WPTurbo_Custom_Nav_Walker, is declared, extending the WordPress built-in Walker_Nav_Menu. The purpose of the custom walker is to modify and expand the functionality of the core Walker_Nav_Menu class.

In our WPTurbo_Custom_Nav_Walker class, we override the start_el method. This function is responsible for generating the markup for each individual menu item. Here, we introduce a condition to differentiate regular menu items and a special ‘search’ item.

function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    if( 'search' == $item->title ) {
        $output .= get_search_form(false);
    } else {
        parent::start_el( $output, $item, $depth, $args, $id );
    }
}

If the title of a menu item is ‘search’, this will replace the menu item with a search form generated by the get_search_form() function – thus adding a search form to the navigation menu. Other items are processed as normal using the method provided in the parent class of Walker_Nav_Menu.

Next, the function wpturbo_add_search_form_to_nav_menu is declared. This function adds a new menu item into the array of menu items to be displayed in the primary location.

function wpturbo_add_search_form_to_nav_menu( $items, $args ) {
    if( $args->theme_location == 'primary' ) {
        $search_item = array( 
            'title' => 'search', 
            'menu_item_parent' => 0, 
            'ID' => 'searchform', 
            'db_id' => '', 
            'url' => '#', 
            'xfn' => '', 
            'object' => '', 
            'object_id' => '', 
            'classes' => array(), 
            'type' => '', 
            'status' => '', 
            'description' => '', 
            'attr_title' => '', 
            'target' => '', 
            'current' => ''
        );
        $items[] = (object)$search_item;
    }
    return $items;
}

This function then hooks into the wp_nav_menu_objects filter, allowing the list of menu items to be modified before they are rendered to the screen.

The wpturbo_use_custom_nav_walker function subsequently alters the arguments passed to wp_nav_menu, replacing the default menu walker with our custom WPTurbo_Custom_Nav_Walker.

The wp_nav_menu_args filter is used to substitute the WPTurbo_Custom_Nav_Walker class as the Walker for menu rendering. As a result, when WordPress builds the nav menu, it now uses the custom methods in WPTurbo_Custom_Nav_Walker due to the applied filter.

Now, whenever the WordPress menu with ‘primary’ as its theme location is displayed, a search item is added to the list of menu items, and if identified by the custom walker class, it is replaced by a search form.

Register an account to save your snippets or go Pro to get more features.