Hook a search form anywhere on the site, using a custom plugin

I need to know how a wordpress search form works.

It’s a form tag with an input that has the name s on the search field.

Likewise example.com/archive/?s=term will work on any post archive and narrow it down to just the search term “term”. There is nothing special about the search form itself.

Likewise searches can be triggered with no search form whatsoever by specifying the s query parameter.

WordPress knows if a page is a search results page based on the is_search function which checks on the s parameter of the main query ( a WP_Query object ).

I’m creating a custom tempa and would like to insert a search form and use a plugin instead of the theme itself to handle requests.

This is what you should have asked about, and you should have provided significantly more detail. Wether the theme or a plugin handles the request is irrelevant here, the code to do it would be almost identical in either case. Aside from theme templates and the order things are loaded in there is zero sandboxing between plugins and themes. A filter works the same wether it’s in functions.php or a plugin.

In summary, my site has a form, it needs to submit a request, and it needs to be captured by the plugin.

This has nothing to do with search other than that it’s conceptually related from a users point of view. Ignore WordPress search it is a red herring and a distraction, a source of confusion. It has nothing to do with your problem or the solution.

You need:

  • a form
  • a place to submit the form, aka the action="... part, maybe a page, maybe the same page, there is no canonical golden solution to this that you should always use, lots of plugins do different things
  • a way to identify when the form is submitted, perhaps a known URL parameter or a hidden input if you’re POST‘ing
    • when it’s submitted run your code and fetch results
    • make sure the name of the field is not reserved, if you reuse a name used by a query variable WP uses it will be picked up and put into the main query, leading to conflicts
    • then display the results, perhaps via a block/shortcode/widget, or loading a custom template, or maybe by submitting to a page which can have a theme template or something else, there is no single answer to this part, there are lots and lots of options with tradeoffs.

At this point, any tutorial on how to build a form in a theme would answer most of your questions and provide vital reference points to let you figure out what it is that you need to do and what you need to know.