Wordpress

How to Add Search Form in your Post with a WordPress Search 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:

1 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:

01 function wpbsearchform( $form ) {
02
03     $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
04     <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
05     <input type="text" value="' . get_search_query() . '" name="s" id="s" />
06     <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
07     </div>
08     </form>';
09
10     return $form;
11 }
12
13 add_shortcode('wpbsearch', 'wpbsearchform');

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s