WordPress: How to add a Search Icon in Menus with toggle effect using jQuery?
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.


Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

SQL Inner JoinSQL/MySQLHow to Use SQL MAX() Function with Dates?SQL/MySQLHow to add a ribbon inside a container using CSS?CSSWordPress: How to get field values in Advanced Custom Fields?WordPressRecursive WITH Queries in Postgres (Common Table Expressions)PostgresThe fundamental difference between HAVING and WHERE clauses in PostgresPostgres