How to Add Search Bar in your Post with a WordPress Search Shortcode

How to Add Search Bar in your Post/page or anywhere with a WordPress Search Shortcode

How to add Search bar in WP by shortcode
How to add Search bar in WP by shortcode

Recently one of our regular users ask if there was 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.

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

add_shortcode(‘wpbsearch’, ‘get_search_form’);

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:

function wpbsearchform( $form ) {

$form = ‘<form role=”search” method=”get” id=”searchform” action=”‘ . home_url( ‘/’ ) . ‘” >

<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’) .'” />

</form>

return $form;
}

add_shortcode(‘wpbsearch’, ‘wpbsearchform’);

We hope that article will help all those who wanted to create a search form shortcode in WordPress.

Post Author: Zahid Farid

Leave a Reply

Your email address will not be published. Required fields are marked *