Opened 6 years ago
Closed 3 months ago
#49553 closed enhancement (maybelater)
Add option to get_search_form() to not include current search query in the form
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | 5.4 |
| Component: | Themes | Keywords: | |
| Focuses: | template | Cc: |
Description
This is a minor enhancement that adds a new option to the argument array of get_search_form(). The new option would enable the user/theme developer to easily output a search form either with (this is the current function) or without the current search query prepopulated in the <input> field of the search form. Right now there is no easy way to include an always-empty search form in a template. For example, the user might prefer to have a regular/empty search box in a sidebar or other secondary location.
In the patch I've added a 'show_query' option that defaults to true.
So when the template calls get_search_form(array('show_query' => false)) then the search form is output with an empty <input> field. (And get_search_query() is not called.)
There is no change in function when a template calls get_search_form() and doesn't include this new option, so there's no impact on existing templates.
Thanks for considering this enhancement!
Attachments (1)
Change History (5)
#3
@
4 months ago
- Keywords close added
Hi @joelhardi,
Thank you for submitting this ticket, sorry no one has got back to you until now! The request to have get_search_form() output a search field without the current query pre-filled is a valid use case for specific design requirements.
However, WordPress provides the get_search_form filter, which allows developers to completely customize the HTML output of the search form. By hooking into this filter, it's possible to modify the form's HTML to remove the value attribute from the search input field (e.g., <input type="search" name="s" value="..." />).
Since the desired functionality can already be achieved effectively through existing extensibility points in WordPress, we believe adding a dedicated parameter to get_search_form() for this specific purpose is not necessary and would add API complexity where a more general solution via filters is already available.
For these reasons, I suggest that we eventually close this ticket as wontfix as developers are encouraged to use the get_search_form filter for such customizations.
#4
@
3 months ago
- Keywords has-patch dev-feedback close removed
- Resolution set to maybelater
- Status changed from new to closed
It's possible to achieve this with existing filters, though I would say it's not obvious and it could have other side effects.
Example of how to do it:
<?php add_filter( 'pre_render_block', function( $render, $block ) { if ( 'core/search' === $block['blockName'] ) { add_filter( 'get_search_query', function( $query ) { return ''; } ); } return $render; }, 10, 2 );
Having said that, since this ticket is 5 years old and we haven't had any other requests for this feature, I'm going to close this to help clean up Trac. Feel free to reopen if you think this enhancement should be added.
proposed path to add show_query option to get_search_form()