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
BlueHost - Recommended WordPress Hosting
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 »

Comments

  1. Arc says:

    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.

  2. Brian says:

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

    Thanks,
    Brian

  3. David Schmeikal says:

    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?

    • Editorial Staff says:

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

  4. Ionut Alexandru says:

    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.

  5. Keith Davis says:

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

  6. Abel Suing says:

    Thanks a lot! very informative! nice!

  7. Geoffrey says:

    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.

  8. Eugenio says:

    Useful technique, thanks for sharing :)

  9. Gretchen Louise says:

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

    And this code worked like a charm!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.