Wordpress

Add search form anywhere in WordPress Site using shortcode

Add search form anywhere in wordpress
If you want to use your wordpress default search form to your specific post, page or other content such as slider, footer, sidebar, etc. You can do this by using shortcode.

Add search form anywhere in WordPress Site

In this article, I will explain how to add the WordPress search form in your post, page or other content by shortcode.

Simply login to your wordpress admin and open your theme’s functions.php file and paste the following code:

add_shortcode('cstmsearch', 'get_search_form');

Then use the shortcode: [cstmsearch]

The above code will display the default search form.

If you want to display a custom search form, then paste the following code to your theme’s functions.php file:

function mycstmsearch( $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('cstmsearch', 'mycstmsearch');

You can add CSS classes and ids to design a form.

[ratings]

Leave a Reply

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