Adding Search Bar in WordPress Main Menu

Featured Video Play Icon

WordPress Tutorial: In simple steps, going to show you how to add a search bar in the WordPress main menu. The following codes are tested by using WordPress Child Theme.

Step 1: Open functions.php file from the child theme.

Step 2: Add this below code

/*========================================== 
Adding Search Box in Main Menu
===========================================*/
add_filter( 'wp_nav_menu_items', 'main_nav_search', 10, 2 );
function main_nav_search($items, $args){
    // if this isn't the primary menu, do nothing
    if( !( $args->theme_location == 'primary' ) )
        return $items;
    // Otherwise, add search form
    return $items . '<li>' . get_search_form(false) . '</li>';
}

Step 3: Save it and you are done.