0

I want to retrieve search results from a search appliance that gives results in JSON format. Interacting with the form below takes the user to a page of JSON data. Instead, I want to take the search.json content, parse it, and display the results on our WordPress site.

<form method="get" action="https://custom-search.test/s/search.json">
    <input type="hidden" name="collection" value="our-stuff">
    <input class="search-input" value="<?php echo get_search_query(); ?>" />
</form> 

I tried editing the action parameter to include the wp_remote_get function like so action=<?php wp_remote_get("https://custom-search.test/s/search.json";?> but I get a php parsing error.

  • Is this search appliance an elastic search cluster? If so there are plugins that integrate into WP_Query that will do that. Otherwise the <form> tag in your question will submit the form to custom-search.test/s/search.json. Remember, a browser requests a page, PHP generates a string of HTML tags, and the browser displays that result, putting a wp_remote_get in the middle of a tag in PHP won't affect future pages, and it doesn't run inside the browser either. Can you provide more information about your custom search, where it's hosted and how it works? – Tom J Nowell Jun 5 '19 at 15:32
  • The search appliance is called Funnelback and I haven't been able to turn up any integration plugins for it. So if I'm understanding this right, I should turn a user search query into a variable, add that variable to the search string in wp_remote_get, and then I should get a response with JSON data? At this point I would need something like jquery to parse the data? – user145567 Jun 5 '19 at 15:53
  • If you were going to do it entirely in the browser, yes you'd need to talk to the Funnelback API with javascript and display the results using javascript. Otherwise you'll need to do a standard WP searchbox, then in PHP, intercept it on an early hook and make all the requests to Funnelback, then display them with PHP on the search.php template. If you're doing this just for this site then Elastic Search would be a better option as it can run on the same server or co-located – Tom J Nowell Jun 5 '19 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy