0

I'm working on a plug-in which will override the search results returned by Wordpress, with results from a custom XML feed. However, I'm not looking to do this on a separate page, but to replace the results Wordpress would normally display on the blogs search page with the results from my XML feed.

I have been unable to find a filter or hook which would allow me to intercept or override the results wordpress returns, nor can I find one which allows me to override what is displayed on the page. My first thought was to look for something similar to the_content, but I'm unable to locate anything similar to that for use in this situation.

The plug-in is going to be used by sales staff, so the plug-in needs to work in such a way that it requires as little work and technical knowledge as possible on the part of installer. My reasoning for wanting to override the results instead of inserting a new widget is that I want the plug-in to use the same search form the theme uses by default.

Is this even possible? If so, how would I go about doing it?

4
  • To be clear. Will you return results from outside of Wordpress? Or just change the search result format to a custom XML feed?
    – Steven
    Commented Aug 18, 2011 at 18:34
  • Why would you over-ride the search results instead of just providing an alternative and disabling the default search.
    – Wyck
    Commented Aug 18, 2011 at 18:34
  • @Steven The results will be coming from outside Wordpress and replaced with results from an XML feed. The plug-in is only meant to be used by the company I'm developing the plug-in for,and they simply want to populate it with their own search results.
    – Wipqozn
    Commented Aug 18, 2011 at 20:47
  • @wyck It's going to be used by sales staff so it needs to be as easy to install as possible, without any manual work required. On the same note I would like for it just use the same search form the theme uses.
    – Wipqozn
    Commented Aug 18, 2011 at 20:48

1 Answer 1

5

You can use template_include filter hook to check if the current call is a search call and if so include your own template in which you can do what ever you want :

add_filter('template_include','my_custom_search_template');

function my_custom_search_template($template){
    global $wp_query;
    if (!$wp_query->is_search)
        return $template

    return dirname( __FILE__ ) . '/my_search_template.php';

}
6
  • My concern with this is that the plug-in needs to be dropped into any WordPress theme without creating a new template page. I came across this method myself and I believe it would require me to create a template page for the results, or choose a fitting template page. As noted above, the plug-in is going to be used by sales staff so that's not an ideal option.
    – Wipqozn
    Commented Aug 18, 2011 at 20:49
  • In that case take a look at the_posts filter hook where you can run the same check if the current query is a search query and if so return your results.
    – Bainternet
    Commented Aug 18, 2011 at 21:06
  • I'll take a look at that and report back, thank you :)
    – Wipqozn
    Commented Aug 18, 2011 at 21:11
  • The Posts was a good idea, but due to the theme I'm working for with it's not the ideal solution. However I ended up using a slightly different solution then expected, and I template_include was a key component in that.
    – Wipqozn
    Commented Aug 22, 2011 at 11:47
  • Are you able to share the solution that you came up with? Commented Nov 7, 2014 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.