There is a lot of speculation about adding a WordPress Search Form.  On the other hand, WordPress provides a lot of filters (and actions) to allow plugins or themes to modify WordPress comportment. Among them, ‘get_search_form’ can be used to modify the search form.

How to Create an Advanced Search Form for WordPress | Michael Soriano

If a theme author codes a search form that does not pass through this filter, no plugin is aware of this search form and thus no plugin can modify it for its own purposes.

Interestingly most people want to know if they can WordPress search forms using PHP code in your post or page content using a shortcode.

Well, the answer to that question is Yes. In this article, we will show you how to add the search form in your post or page content by creating a WordPress search shortcode.

WordPress Search Form Using PHP Code

All you have to do is open your theme’s functions.php file or a site-specific plugin and paste the following code:

1
add_shortcode('wpbsearch', 'get_search_form');

Make sure to understand that this code is primarily used to hard code the search form. It is mainly done to change the search form to a sidebar along with a search widget for WordPress.

Then use the shortcode in your post/page content like so: [wpbsearch]

This will display the default search form. If you want to display a custom search form, then you can do so like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
function wpbsearchform( $form ) {
    $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
    <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
    <input type="text" value="' . get_search_query() . '" name="s" id="s" />
    <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
    </div>
    </form>';
    return $form;
}
add_shortcode('wpbsearch', 'wpbsearchform');

 

Plugin Method

To reduce complication and make configuration easy, as fast as 5 minutes use WP fastest site search plugin by Expertrec

  1. Install WP fastest site search plugin from WordPress plugin store.
  2. Go to https://cse.expertrec.com/newuser?platform=wordpress.
  3. Enter your WordPress website URL and click on next->
  4. The crawl will start in the background.
  5. Login to your WordPress admin panel.
  6. In your left panel you must be able to see expertrec’s WordPress plugin, click on that.
  7. Get your site ID (or) API key from your expertrec control panel ( here ).
  8. Copy paste your site id in your WordPress control panel and click on save changes.
  9. Make sure you have upgraded to a paid plan before 15 days of free trial. (Price begins at 9 USD per month)
  10. That’s it! Your brand new WordPress search is up and running!

Watch this video to learn more about WordPress Search Form

You may also like