Skip to content

Instantly share code, notes, and snippets.

@sergeliatko
Created November 13, 2018 13:27
Show Gist options
  • Save sergeliatko/7cb59ada65f6f3b2f1b4f7c0aade7d2d to your computer and use it in GitHub Desktop.
Save sergeliatko/7cb59ada65f6f3b2f1b4f7c0aade7d2d to your computer and use it in GitHub Desktop.
Replace menu item text with search form
<?php
/**
* Allow search form in menu items. (add CSS class has-search-form to menu item classes)
*/
add_filter( 'walker_nav_menu_start_el', function ( $item_output, WP_Post $item ) {
if ( isset( $item->classes ) && in_array( 'has-search-form', (array) $item->classes ) ) {
$item_output = preg_replace( '/(<a.*<\/a>)/', get_search_form( false ), $item_output, 1 );
}
return $item_output;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment