Add Search Form in Post Content With Shortcode

How to Add Search Form in Post Content With Shortcode

in Shortcode
Google+ LinkedIn

In previous article we have shown you how to create a shortcode for PayPal donation button so you can add a donation button easily anywhere you desire. And in this article we will show you how to add WordPress search form in post content with the help of shortcodes. There are times when you might have want to add a WordPress search form in the middle of post or page content and pasting search form function does not help since WordPress does not allow execution of PHP in post content for security reasons. That’s where shortcodes comes in picture, they allow us to execute predefined PHP code/functions in the middle of post content. And with the help of shortcode we can easily add a WordPress search form in post content.

Following is a simple function to create a shortcode and this shortcode will output WordPress search form in post content on use. So with this help of this shortcode you can display a search form practically anywhere you want on your WordPress website. To use this feature, simply paste the following WordPress code snippet in your theme’s functions.php file and the shortcode will be ready to use.

// add search form in post content with shortcode
function wcs_search_form_shortcode( ) {
    get_search_form( );
}
add_shortcode( 'search', 'wcs_search_form_shortcode' );

To use this shortcode and display WordPress search form simple add this to your post or page content.

[search]

Share the love

If you like this snippet, share it with others!

Leave a Reply

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