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( '/' ) . '" >
<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');
We hope that article will help all those who wanted to create a search form shortcode in WordPress.







Just used it in my WP site. Was able to add search box anywhere I like.
Thank you! This one’s simpler and easy to use compared to the plugins out there.
Great fix for what I need. However, is there any way to make the search box shorter?
Thanks,
Brian
Yes, you can do that by editing the CSS file.
Great little tutorial. I tried to embed this shortcode within a column shortcode, but it just appears at the top of the page, instead of within the column. Is there something I need to add?
Probably has a conflict with the columns shortcode styling and the search shortcode formatting.
i read your site everytime when you put something new. I want to know if is posible to put search form for my site..like google search..Now i used search default from wordpress but i dont like very good the results and i want to know if is posible to put “google search” just for my content. i hope you understand, i try to write correct english but i’m not so good. i wait your answer if is posible. i appreciate your site and your work.
Yes it is possible.
http://www.wpbeginner.com/wp-tutorials/how-to-add-google-search-in-a-wordpress-site/
Thanks for the tut.
Always learn something when I come over.
Appreciate you taking the time.
Thanks a lot! very informative! nice!
Hi,
Thank you for the tip .
Don’t forget to use different IDs to avoid duplication of FOR and ID’s values.
Here you use exactly the same as default WordPress’ search form.
Thank you,
Regards,
G.
Good tip. Yeah, we just copied the code from the core for the sake of demonstration.
Useful technique, thanks for sharing
Thank you so much! Really appreciate the way you guys answer our questions.
And this code worked like a charm!