Skip to content

Instantly share code, notes, and snippets.

@srikat
Last active August 29, 2015 14:06
Show Gist options
  • Save srikat/3f2b436dbec83c627a2d to your computer and use it in GitHub Desktop.
Save srikat/3f2b436dbec83c627a2d to your computer and use it in GitHub Desktop.
A slightly improved 'Add Nav Extras' code for adding Search in Navigation. http://sridharkatakam.com/slightly-improved-add-nav-extras-code-adding-search-navigation/
<?php
//* Do NOT include the opening php tag
add_filter( 'wp_nav_menu_items', 'theme_menu_extras', 10, 2 );
/**
* Filter menu items, appending a search form.
*
* @param string $menu HTML string of list items.
* @param stdClass $args Menu arguments.
*
* @return string Amended HTML string of list items.
*/
function theme_menu_extras( $menu, $args ) {
//* Change 'primary' to 'secondary' to add search form to the secondary navigation menu
if ( 'primary' !== $args->theme_location )
return $menu;
$menu .= '<li class="right search">' . get_search_form( false ) . '</li>';
return $menu;
}
ob_start();
get_search_form();
$search = ob_get_clean();
$menu .= '<li class="right search">' . $search . '</li>';
$menu .= '<li class="right search">' . get_search_form( false ) . '</li>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment