How to add search form in the wp nav menu?

WP nav menu
WP nav menu

While finding solutions and helping others with solutions in the WordPress Support forum, I came across another small and useful piece of code. This one is about adding a WordPress search form to the WordPress navigation menu. Usually there are not many theme which is designed to have the search form in the wp nav menu and like always WordPress is flexible enough to tweak this with few lines of code.

To add a search form to the wp nav menu items, we can use the available filter “wp_nav_menu_items“. This filters allow two parameters with it ie: $items and $args. The first one is a string which lists the menu items available where as the later is an object containing the details of the corresponding nav menu.

We can add the following lines of code to the theme’s functions.php file to make this work, and like always I would suggest to make the following changes using a child theme.

The above code adds the WP search form to the wp nav menu and with some small style changes you can make the search form work with your theme. I had come across this in the support forum where the user was using a hiero theme, so by any chance if you are using the same theme then you can add the below css to the child theme’s style.css file to make it perfectly work with the theme

We can also use the wp_nav_menu_items filter to add a new wp nav menu item. This may or may not be a static item and may or may not be made available for logged in users with adding some conditions.


Maruti Mohanty

I am a happy go lucky WordPress Developer, who loves biking, movies and learning different languages


2 Responses

  1. Vinod says:

    Nice tutorial. This easy to use free plugin also does the same job.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: