How can I reuse the WordPress search form for a custom plugin / widget?

I looked into doing the same thing and in the end opted for a custom solution using thickbox as being less complex. Could be it’s possible, but perhaps not worth the effort. The code is certainly not built for customisation as far as I could see.

  1. Open a thickbox modal with a search field
  2. Ajax search the post title with the user-entered search term
  3. Echo the results into the modal as a list/table of post title, adding the post ID as a data attribute e.g. to a checkbox if the user can choose multiple items
  4. When the user clicks the ‘select’ button, use js to get the checked items and put them in a list/table in the page with the post id in a hidden field that you can access when the post is saved.

A couple of other notes:

  • In my case searching the post title was sufficient, I used a custom query with $wpdb -> prefix . 'posts.post_title LIKE %s.
  • explode() and loop through each of the search terms, using each with '%' . $str . '%' in the query