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 am using the Twenty Twelve theme, and the following is the code it uses to register and display the menu.

In functions.php:

function twentytwelve_setup() {

    ...

    // This theme uses wp_nav_menu() in one location.
    register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ) );

    ...

}
add_action( 'after_setup_theme', 'twentytwelve_setup' );

In header.php:

<nav class="site-navigation main-navigation" role="navigation">
    <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
</nav>

And the output code looks similar to this:

<nav class="site-navigation main-navigation" role="navigation">
    <div class="menu-main-menu-container">
        <ul id="menu-main-menu" class="menu">

            <li id="menu-item-9" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-9">
                <a href="http://aahank.com/">Home</a>
            </li>

            <li id="menu-item-12" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-12">
                <a href="http://example.com/">About</a>
            </li>

        </ul>
    </div>
</nav>

But I would like to modify the output so that a search box can be integrated into the navigation menu as a menu item like so:

<nav class="site-navigation main-navigation" role="navigation">
    <div class="menu-main-menu-container">
        <ul id="menu-main-menu" class="menu">

            <li id="menu-item-9" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-9">
                <a href="http://aahank.com/">Home</a>
            </li>

            <li id="menu-item-12" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-12">
                <a href="http://example.com/">About</a>
            </li>

            <li id="menu-item-13" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-13">

                <!-- Search box inside nav as a menu item -->
                <form method="get" id="searchform" action="/">
                    <label for="s" class="assistive-text">Search</label>
                    <input type="text" class="field" name="s" id="s" placeholder="Search" />
                    <input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />
                </form>

            </li>

        </ul>
    </div>
</nav>

How do I go about doing this? I need a code-backed pointer or two. Spoon-feeding is welcome too. :P

share|improve this question

3 Answers 3

up vote 2 down vote accepted

based on : http://wpfirstaid.com/2010/10/extend-the-wordpress-menu/

function wpa_58902($items){
    $search = '<li class="search">';
    $search .= '<form method="get" id="searchform" action="/">';
    $search .= '<label for="s" class="assistive-text">Search</label>';
    $search .= '<input type="text" class="field" name="s" id="s" placeholder="Search" />';
    $search .= '<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" />';
    $search .= '</form>';
    $search .= '</li>';

    return $items . $search;
}
add_filter('wp_nav_menu_items','wpa_58902');
share|improve this answer
    
more info to: cmsblogheart.wordpress.com/2014/03/19/… –  ravi patel Mar 20 '14 at 12:17

I like better this solution:

In header.php:

</hgroup>
<div class="header-search"><?php get_search_form(); ?></div>

In style.css:

.header-search {
    float: right;
    margin-top: -50px;
}
@media (max-width: 650px) {
    .header-search {
        margin-top:5px;
        text-align:center;
        float:none;
    }
    .main-navigation  {
        clear:both;
    }
}
share|improve this answer

I know this is a long time after the question, but I had the same issue as mentioned in the question. I found no suitable solution in tutorials or on any stackexchange and in the end gave up and wrote my own code for it. I've since published it as a plugin:

Bop Search Box Item Type For Nav Menus

Crucially, this adds the search box as a menu item option like any other in the menu admin screen, and so no code needs to be added.

If you're after a more bespoke solution to you, there are some hooks that can be used (as detailed in the FAQ). Also, it's a very small, so you may want to use the code to make your own plugin, if you like.

share|improve this answer

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.