This is fine, but you if you want things to look “cleaner” and be easier to modify you could instead create a new file menu-search.php and add your HTML there then tweak your function a bit (don’t forget to prefix your functions)
function myprefix_add_search_box_to_menu($items, $args) {
if ( $args->theme_location == 'menu-1' ) {
ob_start();
get_template_part( 'menu-search' );
$items .= ob_get_clean();
}
return $items;
}
add_filter(‘wp_nav_menu_items’, ‘myprefix_add_search_box_to_menu’, 10, 2);
Related Posts:
- How can I hook into and edit the text of a wp_nav_menu tag?
- Rewriting search and pagination base
- Run a filter when a walker runs
- Change Navigation Bar based on logged in or not
- Add data attribute to each li in menu
- How to add an arrow to menu items has submenus
- Unique icons next to each WordPress menu item
- Add item to top of menu using a filter in functions.php
- Get the name of menu item with wp_nav_menu
- How to add aria role and schema markup to custom walker container
- How can I modify this code to make the search box include tags and meta
- How to disable sub menu items from being created?
- Select All not working in a WordPress search filter
- Search box background on a different page template
- How to add different menu items on different menus?
- Menu — How to add “current-menu-grand-ancestor” css class
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- WP Walker using custom css classes
- How to make a search button that will search my website?
- WordPress Search Results for Multiple Post Types
- WP Query with custom Shortcode
- add_action shortcut?
- Change list item content in menu navigation to add a child element with specific class
- Is it acceptable to treat a filter like an action?
- Menu Limitations
- How can I Add a variable PHP in the Menu Nav
- Customising the search function?
- Where to place PHP code that inserts (or doesn’t insert) menu item?
- How can I remove a function that has been added to wordpress with add_filter?
- Adding an Avatar to the Top Nav Bar
- How to control WordPress image metadata (using Imagick)?
- How to add seperate classes to no-search-result and found-search-result pages on wordrpess search – is_search()
- Edit incorrect password message WordPress
- How do I add custom bulk actions to multiple custom post types?
- Automatically adding new post categories to menu
- Show prices with tax in Woocommerce Mini Cart [closed]
- Ajax category filtering products default show all
- Add a specific part of current category page url to shortcode
- How to get the element ID from new menu list that added with add_filter()?
- Why does my callback function not work with this custom filter hook?
- Custom search results page not working with empty search
- Contact Fom 7 – how to add custom HTML inside span.wpcf7-form-control-wrap AND IMMEDIATELY AFTER input.wpcf7-form-control?
- Removing “wpautop” (auto tags) only on certain pages?
- Remove metabox from WordPress menu editor page?
- WordPress default Search function inconsistent in WooCommerce Product Titles
- Add filter when image is uploaded?
- Custom Walker_Nav_Menu issue with variables on PHP 7.3
- Remove the first 5 characters of the_title and orderby that
- Remove a div from RSS feed
- Print last modified date only on posts
- WordPress Gravatar filter is removing my custom attributes
- Save Search System
- wp_nav_menu returns menu list in ascending order. How can I arrange the menu same as dashboard menu
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- Seach custom post type posts only by meta fields?
- Child page menu in sidebar
- Title Case WordPress Menu Items
- Search not showing all results
- Autocomplete search box
- Secondary navigation menu on one page
- Remove class in nav_menu_link_attibutes filter
- search suggest – filter post type
- WordPress Quick Question . How to Get Parent Link in Submenu in My Code
- Trying to store submenu items to render out after main menu
- Change tinyMCE editor to allow marked content when pasting from Word
- edit search.php to include subdomain posts
- Detecting classes, adding widgets, and adding divs in with a Nav Walker
- Search Query: how to construct a search string from two select elements?
- Problem with login form
- How to change menu order item
- Can anyone offer any help with this function?
- Custom Menus: dynamic highlighting problem with custom home link
- get_the_title() gets printed out twice
- Redirect to another page using contact form 7? [closed]
- Alter required message using comment form api
- How to debug this search & replace strings snippet?
- Hide Featured Image Meta Box on Editor Screen
- Adding PHP in the menu
- Two menus show up
- Add filter multiple times using only one master function
- How to edit php code in WordPress Post file?
- Change page title from page using php via php executed from page/post itself
- How to make modifications only to certain elements of an HTML string on the server-side?
- Override a function defined in wp-includes/comment-template.php
- Search WordPress using static html page
- Exclude posts with specific metadata from search?
- Register a menu – Error Header
- How to style one item from main navigation?
- Use Tags to initiate Search
- Search form does not work in my website
- Show About and Contact Us page when they’re clicked in the top menu.
- Filtering a function’ output for a new continued function
- New walker for walker_nav_menu to change inside container data
- Add data-id attribute to child page links
- Parsing Menu Items and Blog Posts
- Add login hyperlink to secondary navigation menu
- Deleting Certain terms from appearing on the front end as links
- Content filter won’t work
- How to remove the message ‘We could not find any results for your search’ without changing template files and without adding posts/pages?
- Automatically add images to a menu