How to automatically add a search field to your navigation menu

Open your functions.php file, and paste the following code. The search field will be displayed once you saved the file.

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;
}

Thanks to Ronald for the cool tip!

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

How to automatically add a search field to your navigation menu

Related posts:

  1. Automatically add a search field to your navigation menu
  2. WordPress tip: Remove WP 3.1 Admin Bar
  3. How to set HTML editor as the default in WordPress
  4. WordPress trick: Get category slug using category ID

Leave a Reply