I would like to add a search box to the nav menu by appending the following code in functions.php (at the end of the file, but before ?>).

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;

However, the search box doesn't appear.

EDIT: Actually, there is no problem with the above code. While clicking the search icon (PS: the theme I use is Radiate), two search boxes appear.

What I expect is to place the search box inside the nav menu, instead of a click is needed to show the search box:

enter image description here

PS: here is the searchform.php:

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
        <span class="screen-reader-text"><?php _ex( 'Search for:', 'label', 'radiate' ); ?></span>
        <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search &hellip;', 'placeholder', 'radiate' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s">
    <input type="submit" class="search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'radiate' ); ?>">

Your code works perfectly for me as is. The only thing I can think of is that you have a searchform.php defined in your theme but it's empty? (If searchform.php doesn't exist in your theme, WP will use a default form.)

  • Thx. Yeah, no problem with the above code. I update my question, please check it. – SparkAndShine Oct 16 '15 at 0:01
  • 1
    Okay that sounds like a theme behavior issue - have you tried looking through searchform.php for the html elements, and the themes css rules that apply to the form? You'll probably just have to edit those directly. – Benjamin Love Oct 16 '15 at 0:08
  • thx again. I paste the code of searchform.php to the question. – SparkAndShine Oct 16 '15 at 11:25
  • You seem to be fishing for someone to do your work for you. The search field is being rendered, so your original question is resolved. Now you're just dealing with css/javascript behaviour issues that have very little to do with wordpress. If you have no idea where to start, you should hire a developer to help you. – Benjamin Love Oct 17 '15 at 14:50
  • thx for ur kind reminder. – SparkAndShine Oct 17 '15 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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