Forums Forums Search & Filter Pro Any way to pre-populate search field?

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Anonymous
    #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
    #54675

    I 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
    #54728

    Hi 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
    #54729

    My 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_Object

    Anonymous
    #54735

    Awesome! 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;
    }
    
    Trevor
    #54737

    Can I close this thread and mark it resolved?

    Trevor
    #56321

    As I haven’t heard back from you for a while, I am closing this thread.

Viewing 7 posts - 1 through 7 (of 7 total)