I have already asked a similar question at stackoverflow (https://stackoverflow.com/questions/22492292/add-filter-in-wordpress-how-do-i-know-if-it-is-run), but maybe that was the wrong place?
As you can see there I am trying to add a search box in the way I have seen recommended:
add_filter('wp_nav_menu_items','menu_search');
function menu_search($items){
$search = '<li class="search">';
$search .= '<form method="get" id="searchform" action="/">';
$search .= '<input type="text" class="field" name="s" id="s" placeholder="Search" />';
$search .= '</form>';
$search .= '</li>';
return $items . $search;
}
However I see no trace of the search box in the menu. In fact it looks like the code added to add_filter is not even run.
There are a couple of things I do not know here. I am doing this in a new theme, based on the underscore theme (see http://underscores.me/) since I needed a really light weight theme. I am starting to wonder if the code for filtering is run in the underscore theme. What is the best way to check that? (I do not know php debugging at all. I just added Debug Bar etc.) And did I miss something in the underscore theme here? I mean should I have added anything for it to run the filtering?
wp_nav_menu_items
aren’t mentioned in the codex, so there are no concrete description on how to use it.wp_nav_menu_items
can be found on line 347 inwithin
wp_nav_menu
.In the codex, it states the following
So this means that
wp_nav_menu_items
is only fired when a navigation menu is present that was created in the “menus” panel, and not on the default navigation menu. It is a design flaw in my opinion.wp_nav_menu_items
should actually be included in the default menu as well. This is also one aspect that is not mentioned in any of the tutorials I’ve tested.There is another filter that I’ve tested,
wp_list_pages
, that seems to work as expected.There is a problem with your code though as well. When I add it to the default nav bar, any searches I do sents me to my db. Actually again. I could not get proper examples to work. I eventually found this code in a plugin called search-box-on-navigation-menu.
For this code to work on the default nav bar, just add the following line to the code
add_filter('wp_list_pages','add_search_box', 10, 2);
, so your end code will beHope this help
You can also use this
This is a much simpler solution – add the following to your functions.php file:
In this function, you will see that the IF statement is checking for a desired location – specifically, this example is checking for the Primary Menu:
You could also use something like this to determine which menu:
If there is a primary menu, it returns whatever you have in the “return $items” quotes:
In this case, I created a list-item and concatenated the get_search_form() WP function.
Most importantly, for this to be displayed in your menu as a list-item, you need to set the get_search_form() to false:
This is because of the follow argument in the get_search_form() function:
This will successfully achieve the desired search form in any particular menu.