Most users tend to ask if there is an easy way to add the WordPress Search Form 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 WordPress search form in your post or page content by creating a WordPress search shortcode.

In order to make changes in the WordPress search form using PHP code, you can find the file either in the parent or child directory. Find it from there and lead it.

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');

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

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

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');

 

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!
     

Add great search to your wordpress site at 9 USD per month

For more details on Search Form, view the video

You may also like