<?php
add_filter('wp_list_pages','add_search_box', 10, 2);
add_filter('wp_nav_menu_items','add_search_box', 10, 2);
function add_search_box($items, $args) {
ob_start();
get_search_form();
$searchform = ob_get_contents();
ob_end_clean();
$items .= '<li>' . $searchform . '</li>';
return $items;
}
?>
add search box to menus – https://wordpress.stackexchange.com/questions/138437/debugging-trying-to-add-search-box-to-menus
Для темы Storefront
Если есть еще боковое меню или меню в футере, а нужно добавить поиск только в главное меню
нужно добавить условие ‘primary’ == $args->theme_location
// add search box to menus
add_filter('wp_list_pages','add_search_box', 10, 2); // to work on the default nav bar
add_filter('wp_nav_menu_items','add_search_box', 10, 2);
function add_search_box($items, $args) {
if ( 'primary' == $args->theme_location ) {
ob_start();
get_search_form();
$searchform = ob_get_contents();
ob_end_clean();
$items .= '<li class = "my_search">' . $searchform . '</li>';
}
return $items;
}