Yogesh Chauhan's Blog

WordPress: How to add a Search Icon in Menus with toggle effect using jQuery?

in jQuery & WordPress on February 21, 2021

We learned how to use add_filter to hook a function or a method to a specific filter action.

We also learned to use applt_filters to call the functions that have been added to the filter hook.

Finally, we learned to modify navigation menus. In this post, we are going to use the same hook function to modify the menu and create the toggle effect.

Let’s use the same examples code to add the search icon in the menus.

WordPress function.php code

add_filter('wp_nav_menu_items', 'add_search_in_menu', 10, 2);

function add_search_in_menu($items, $args) {
if( $args->theme_location == 'primary' )
       $items .= '<li class="search"><a class="search-icon"></a><div class="search-form">'. get_search_form(false) .'</div></li>';
       return $items;

You can change the menu id and replace it with your WP menu id.

Style the form and icon

Note: This style is based on other elements in the menus as well as above and below the menus. Style your elements accordingly.

  display: none;
  width: 100%;
  position: absolute;
  right: 0;
  top: 100%;
  z-index: 9999;
  background: #fff;

.search-form input[type="search"] {
  width: 90%;
  height: 35px;
  padding-left: 3%;
  background: #d41242;
  border: none;
  border-left: 3px solid white;
  color: rgb(231, 230, 230);

.search-form input[type="submit"] {
  border: 1px solid white;
  padding: 11px 12px;
  border-radius: 0px !important;

  background-image: url('your-icon-path');
  background-repeat: no-repeat;
  background-position: center;
  background-color: transparent;
  background-size: 15px;

As you have noticed in the style above, we have the form display property set as none to hide the form initially. We will use jQuery to toggle the form.


jQuery has .slideToggle() function. We are going to use it to show and hide Read more about it here.

jQuery(document).ready(function($) {
  $(".search-icon").click(function() {

Go through this post to learn how hide a DIV on clicks outside of it using jQuery.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
the box-sizing property in CSSCSSFunction Scope in JavaScript for BeginnersJavaScriptControl Scrolling with CSS Scroll SnapCSSHow can one check to see if a remote file exists using PHP?PHPHow to Secure Web or Mobile Browsers?MiscellaneousHow to make a curtain slider using jQuery and CSS?CSS