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?
- start_lvl Ignored in Custom walker_nav_menu
- wp_delete_auto_drafts() deletes links in menus
- Check if a menu is empty?
- Walker class: Problems with understanding how walk() method can be called without error
- Change comments form title on a page by page basis
- Display only text to WordPress loop without loosing the text formatting
- Remove class that has been added by parent theme
- How can I access the “description” of a menu item?
- append PHP function to the_content
- Adjust the results quantity for Search Results page pagination
- How do I know what variables are passed in a filter/action and what their meaning is?
- Find variables available at a given hook
- How do I get images (with a thumbnail preview) to show in search results?
- Creating search filter through plugin
- Woocommerce filter cart and category specific quantity [closed]
- items_wrap not working
- Add relevant tag to search results
- WooCommerce: Add Payment Gateway Field to Webhooks [closed]
- Have CSS Class Added with `is_page()` WP function
- How to call function at the bottom of post using plugin?
- Use wp_get_recent_posts with search term
- Problem adding class to body_class
- have_posts() execution failure
- Can’t search posts using WP_QUERY inside AJAX Function
- Get_avatar filter?
- Nesting if statements to echo only one string based on what tag was filtered?
- How to properly loop through these external URLs to get them into the sitemap using this hook
- WP Walker using custom css classes
- WordPress Search Results for Multiple Post Types
- WP Query with custom Shortcode
- Change list item content in menu navigation to add a child element with specific class
- How do I add custom bulk actions to multiple custom post types?
- Add filter when image is uploaded?
- Remove a div from RSS feed
- Child page menu in sidebar
- Title Case WordPress Menu Items
- Trying to store submenu items to render out after main menu
- How to change menu order item
- Alter required message using comment form api
- 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
- Parsing Menu Items and Blog Posts
- Create page template via functions.php?
- How to have different site identity logos on each page on Astra Theme [closed]
- How to display different submenus?
- Why can’t I add a custom image in my navigation?
- Remove the Tag from wp_nav_menu
- How to internationalize header image?
- apply_filters with several different values?
- Free search and custom taxonomy query not providing the same result
- How to add a custom filter (by coding) before access one wordpress page ? And where to call the custom filter?
- Replace a single variable with add_filter
- Adding an Anchor Link to a wordpress menu using WPBakery Page Builder
- meta_query search names when they have middle initials
- Shortcodes not processing inside post content
- How to put 2 php codes in functions.php without site crashing
- I want to hide “sold by” on certain pages with id page 43
- What is the correct way to search 3 custom fields only in WordPress?
- wp_get_nav_menu_items() with ACF
- How to switch between two primary menus (programatically0
- Used a code to revoke some dashboard menus for my contributors but for some reason it’s revoked me access to the editor [closed]
- How to give custom roles the capability to edit one Menu instead of every Menu
- Primary navigation menu with timed items from custom post type
- How to filter specific element of an array in wordpress/php?
- How to generate customized filtered output for the gallery
- Add a leading zero to pagination
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- How limit the width of a fixed menu-bar on scroll?
- How to apply filter
- Two theme locations for two menus, but only one is showing up
- Contact form field in wordpress menu
- How to call a certain object/menubar in a PHP file
- Remove all nav menu classes except those which begin with certain letters
- Make the text field in search form optional
- Custom field in title
- WordPress – show all posts + ones with specific tag
- How to change the menu color
- Nav Menu Display
- Adding a unique class to wp_nav_menu
- How to strip/remove all blank spaces at the beginning/end of a search string
- Blog Posts not sorted and pagination not adjusted after filtering some category
- How to change wp-admin and wp-login urls
- Product Search in search.php