Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have already asked a similar question at stackoverflow (http://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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
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 Mar 21 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. –  Pieter Goosen Mar 21 at 13:35
    
I agree @Pieter. –  Leo Mar 21 at 19:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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