Adding search to the wordpress menu

Posted on 16th September, 2017 Leave a Comment

This eample is adding a search box to the end of the nav with the id menu-2:

/**
 * Add search box to secondary menu
 */
function wpgood_nav_search($items, $args) {
    // If this is the secondary menu?
    if( ($args->theme_location == 'menu-2') ) 
    // Add the form
    return $items . '<li class="nav-search"><i class="fa fa-search"></i><i class="fa fa-times"></i></li>';
    // Otherwise just return the nav items
    return $items;
}
add_filter('wp_nav_menu_items', 'wpgood_nav_search', 10, 2);

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]