Create search form with shortcode in WordPress – Learn WordPress from a to z

Tutorials 0 lượt xem

Want to add a search form to a post, page, or sidebar of your website?

Adding a search form to your post or page content is a good way to encourage users to find more content and stay on your site longer.

Today I will show you how to create a search form using shortcode in WordPress.

create search form with shortcode

Why do you need to add a search form to your website?

If users already enjoy reading a post on your blog, then they will probably want to read more. Displaying a search form on your website will help them find more content to read. This also helps you to increase page load and reduce bounce rate.

In this article, I will show you how to add a WordPress search form to your website. Let’s start with me!

Create search form with shortcode

First, edit the file functions.php of the theme you are activating. Add the following code:

add_shortcode('wpbsearch', 'get_search_form');<div><span>1</span></div>

Then just add the shortcode [wpbsearch] to your post, page, or anywhere you want the search form to appear.

create search form with shortcode

If you want to display a custom search form, then you can use this code instead.

function wpbsearchform( $form ) {
   
    $form = '

‘;

return $form;
}

add_shortcode(‘wpbsearch’, ‘wpbsearchform’);

12345678910111213

You can now add a placehoder to the input box, or change the label of the submit button.

In addition, you can also use CSS to customize the interface accordingly.

Epilogue

Hope this tutorial will help you to know how to add search form using shortcode.

If this article was helpful and saved your time, please help me share it. Also if you are interested in similar topics, read other WordPress Tips articles and follow Fanpage so you don’t miss new posts from me.

Bài viết liên quan