Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:58:57+00:00 2026-06-02T04:58:57+00:00

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

  • 0

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">
</form>
<ul id="menu-main-menu">
  <li><a href="#">Home</a></li>
  <li><a href="#">Sample Page</a></li>
  <li></li>
</ul>

</div>

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:58:59+00:00Added an answer on June 2, 2026 at 4:58 am

    If your theme has a searchform.php, you’re probably hitting Ticket #16541: get_search_form() ignores $echo if searchform.php exists.

    Your workaround is fine if you’re just echoing the search form anyway, but if you need to capture the output in a variable, you can work around this with output buffering:

    <?php
    ob_start();
    get_search_form();
    $my_search_form = ob_get_clean();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a menu system using a UL/LI structure. I'm trying to use
I'm creating several menu items programmatically like this : NSMenuItem* newItem = [[NSMenuItem alloc]
I was creating a simple accordion menu based on concept used in here http://jsfiddle.net/WMfsR/
I am creating a menu that opens and closes using jQuery. In simple terms,
I'm attempting at creating a menu from a table using the Suckerfish css menu
I am creating a dynamic menu of users from the database. When the user
I am creating a menu in Android and I would like this menu to
This is my code for creating my menu for the program: WNDCLASS wc; ...
I need to hide the Home link that's generated from Wordpress main menu on
all I am creating my menu list in footer dynamically. now in one row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.