1

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?

3 Answers 3

1

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 in

wp-includes/nav-menu-template.php

$items = apply_filters( 'wp_nav_menu_items', $items, $args );

within wp_nav_menu.

In the codex, it states the following

Displays a navigation menu created in the Appearance → Menus panel.

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.

<?php
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;
    }
?>

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 be

<?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;
}    
?> 

Hope this help

3
  • 1
    Thanks Pieter, yes, this is essentially what I did though I did not had time to tell it here yet, but I added to wp_page_menu instead of wp_list_pages. It took me some time finding the bug in get_search_form :-(. (This is a question of quality and I was surprised.) And I agree it is a flaw not merging the code. It would save quite a few people time if the code was merged. Quality matters and save times, makes it possible for more people to contribute!
    – Leo
    Commented Mar 21, 2014 at 13:30
  • @Leo this is actually something that needs to be addressed and should be a suggestion for the next update of wordpress core. Please mark my answer as accepted if it is a solution to the problem so that people finding their way here via a search engine will also have the pleasure of using this solution. Commented Mar 21, 2014 at 13:35
  • I agree @Pieter.
    – Leo
    Commented Mar 21, 2014 at 19:23
0

You can also use this

add_filter( 'wp_nav_menu_items','add_search_box', 10, 2 );
function add_search_box( $items, $args ) {
    $items .= '<li>' . get_search_form( false ) . '</li>';
    return $items;
}
2
  • 1
    Your answer has been flagged as low quality. Dumping code as an answer does not make for good answers. Please file an edit and explain how your code works and where it should be used Commented Nov 2, 2014 at 18:32
  • how to add as first li (in search form) ?
    – Ravi Patel
    Commented Jun 15, 2015 at 7:48
0

This is a much simpler solution - add the following to your functions.php file:

add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);
function add_search_box_to_menu( $items, $args ) {
    if( $args->theme_location == 'primary' )
        return $items."<li class='menu-header-search'>".get_search_form(false)."</li>";
    return $items;
}

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:

if( $args->theme_location == 'primary' )

You could also use something like this to determine which menu:

$args->menu->slug == ‘the_menu_slug’

If there is a primary menu, it returns whatever you have in the "return $items" quotes:

return $items."<li class='menu-header-search'>".get_search_form(false)."</li>";

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:

get_search_form(false)

This is because of the follow argument in the get_search_form() function:

$echo (bool) (Optional) Default to echo and not return the form. Default value: true

This will successfully achieve the desired search form in any particular menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.