Add search form anywhere in WordPress Site using shortcode

Last Updated on

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.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Leave a Reply

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

More in Wordpress
Change Sender Name and Email Address
How to Change Sender Name and Email Address in Outgoing WordPress Email

By default, WordPress uses WordPress as From name and wordpress@yourdomain.com as the email address for all outgoing WordPress notification emails.

Close