Forums › Forums › Search & Filter Pro › Any way to pre-populate search field?
Tagged: pre-populate
- This topic has 6 replies, 2 voices, and was last updated 8 years, 3 months ago by Trevor.
-
Anonymous(Private) August 15, 2016 at 11:39 pm #54652
Is there a way to load up a search/filter form and have the search text box pre-filled with something?
Here’s the scenario:
Someone starts a search for ‘thailand’ on our site. This defaults to google search results:
http://asiafoundation.org/search/?q=thailand
Now notice there’s a link to reveal the advanced search form.
I’d like that form to appear with ‘thailand’ pre-filled in the text area.
Right now the form is being rendered in PHP like so:
do_shortcode(‘[searchandfilter id=”44867″]’);
Any hints?
Trevor(Private) August 16, 2016 at 9:05 am #54675I am sure that with some custom (probably javascript) coding, it might be possible, but not by me. However, what importance is there to the intermediate step of the Google search. Why not go straight from the search to the Advanced results for Thailand? If it is a question of relevance order, then you could add Relevanssi to your site and then enable support for that in our plugin’s advanced section.
Anonymous(Private) August 16, 2016 at 5:12 pm #54728Hi Trevor,
Good question. The reason we use google search results first is that google searches not only our site but also inside the 1,000 or so PDF publications we have on our site. WordPress (and advanced search) can’t get in there.
It sounds like we might have to hack the plugin a little to pre-populate the search field. The easiest way might just be by adding an option to the shortcode, e.g.
[searchandfilter id=”123″ searchterm=”thailand”]
We of course don’t expect you to do custom programming for us (unless you are willing to do this as a paid job?) — but if you could point me in the right direction, e.g. which file & function I should look at for adding some options to the shortcode — that would be super.
Thanks!
Trevor(Private) August 16, 2016 at 5:22 pm #54729My guess is that you could do this in the child them functions.php file.
Test if you are on the search page
Test if it has a search term
Get the search term
Use this to change the form? https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Filter_Input_ObjectAnonymous(Private) August 16, 2016 at 5:48 pm #54735Awesome! That was a huge help, thanks. In case anyone else is reading this to solve the same problem, here’s how I solved it:
We happen to have a page-search.php template in our child theme. So, I just added this to the very top:
$searchFilterID = 44867; add_filter('sf_input_object_pre', 'insert_search_term_into_advanced_search', 10, 2); function insert_search_term_into_advanced_search ($input_object, $searchFilterID) { $q = isset($_GET['q']) ? htmlspecialchars($_GET['q']) : ''; if($input_object['name']=='_sf_search') $input_object['attributes']['value'] = $q; return $input_object; }
-
AuthorPosts