Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

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

Last updated on by
Follow WPBeginner on YouTube
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:

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.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • sandeep kumar dan

    Hi, I have just used this script on my theme. Thanks for the help. And its working. But i have one problem, I want to edit the title of that search form how to do. Please help me out.

    Thanks
    Sandeep Kumar Dan

  • Carol Dunlop

    Awesome tip! Thanks for posting, it is just what I was looking for. I wanted something simple so that people can search through exercise videos and this fit the bill.

  • Greg Maldewin

    Great article! I created a short code and then tweaked the CSS to position and size it the way I wanted.

    add_shortcode(‘prodSearch’, function($atts) {
    global $woocommerce;

    echo ”;
    get_product_search_form();
    echo ”;

    });

    • Nick Lewis

      Greg… what was your custom form code for your woocommerce search form you mention here?

      I am trying to get two search boxes into a top navigation one for standard wordpress site search the other for woocommerce products..

  • Arc

    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.

  • Brian

    Great fix for what I need. However, is there any way to make the search box shorter?

    Thanks,
    Brian

    • http://www.wpbeginner.com Editorial Staff

      Yes, you can do that by editing the CSS file.

  • David Schmeikal

    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?

    • http://www.wpbeginner.com Editorial Staff

      Probably has a conflict with the columns shortcode styling and the search shortcode formatting.

  • http://www.facebook.com/profile.php?id=100001784979835 Ionut Alexandru

    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.

  • http://www.facebook.com/profile.php?id=100003862505833 Keith Davis

    Thanks for the tut.
    Always learn something when I come over.
    Appreciate you taking the time.

  • http://twitter.com/abelrsuing Abel Suing

    Thanks a lot! very informative! nice!

  • Geoffrey

    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.

    • http://www.wpbeginner.com Editorial Staff

      Good tip. Yeah, we just copied the code from the core for the sake of demonstration.

  • Eugenio

    Useful technique, thanks for sharing :)

  • http://twitter.com/GretLouise Gretchen Louise

    Thank you so much! Really appreciate the way you guys answer our questions.

    And this code worked like a charm!