WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating a menu manually and trying to include the search form.

this is taken from the Wordpress Docs "Building a simple menu list" at http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items.

I have added an additional <li> element before completing the menu and added the search form.

            // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
            // This code based on wp_nav_menu's code to get Menu ID from menu slug

            $menu_name = 'main-menu';

            if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {

                $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

                $menu_items = wp_get_nav_menu_items($menu->term_id);

                $menu_list = '<ul id="menu-' . $menu_name . '">';

                foreach ( (array) $menu_items as $key => $menu_item ) {
                    $title = $menu_item->title;
                    $url = $menu_item->url;
                    $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
                $menu_list .= '<li>' . get_search_form(false) . '</li>';
                $menu_list .= '</ul>';
            } else {
                $menu_list = '<ul><li>No Menu</li></ul>';
            echo $menu_list;

The problem is in the output.

<div class="mainnav" role="navigation">

<form role="search" method="get" id="searchform" action="/">
  <input type="text" value="" name="s" id="s" size="40" placeholder="Search" alt="Site Search">
  <input type="submit" id="searchsubmit" value="Search">
<ul id="menu-main-menu">
  <li><a href="#">Home</a></li>
  <li><a href="#">Sample Page</a></li>


Could someone tell me why the search is printing before the menu even though I specify echo = false?

share|improve this question
up vote 2 down vote accepted

this might be the cause:

The $echo parameter is ignored when searchform.php is present. There is a issue in the WordPress Trac concerning this.

from the codex: http://codex.wordpress.org/Function_Reference/get_search_form#Notes

share|improve this answer
that'd explain it! Thanks – php-b-grader Feb 26 '12 at 21:18

Your Answer


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.