Creating Custom Search Forms in WordPress Themes

Posted on 21st June 2023

Creating Custom Search Forms in WordPress Themes

One of the most common features requested by WordPress users is the ability to customize their search forms. This is understandable, as the default search form provided by WordPress is very basic.

Fortunately, there are a few ways to go about customizing search forms in WordPress themes. In this article, we will show you how to create custom search forms in WordPress themes.

Creating a Custom Search Form in WordPress Themes

The easiest way to create a custom search form in WordPress is to use the get_search_form() function. This function allows you to specify a file that contains your custom search form.

All you need to do is create a file called searchform.php in your theme’s directory. You can then put your custom search form code in this file.

Here is a basic search form that you can use:

<form role="search" method="get" class="search-form" action="”>

<input type="search" class="search-field" placeholder="” value=”” name=”s” />

<input type="submit" class="search-submit" value="” />

Once you have created your searchform.php file, you can then use it in your theme by calling the get_search_form() function:

If you want to use a custom search form in a widget, you can use the WP_Widget::form() method:

Creating a Custom Search Form Without Using get_search_form()

If you don’t want to use the get_search_form() function, you can create a custom search form without it.

To do this, you will need to use the WordPress search_template() function. This function allows you to specify a custom search template file.

All you need to do is create a file called search.php in your theme’s directory. You can then put your custom search form code in this file.

Here is a basic search form that you can use:

<form role="search" method="get" class="search-form" action="”>

<input type="search" class="search-field" placeholder="” value=”” name=”s” />

<input type="submit" class="search-submit" value="” />

Once you have created your search.php file, WordPress will automatically use it when displaying search results.

Creating a Custom Search Form for a Specific Post Type

If you want to create a custom search form for a specific post type, you can use the post_type_archive_title() function. This function allows you to specify a custom search template file for a specific post type.

All you need to do is create a file called post-type-archive-{post-type}.php in your theme’s directory. You can then put your custom search form code in this file.

Here is a basic search form that you can use:

<form role="search" method="get" class="search-form" action="”>

<input type="search" class="search-field" placeholder="” value=”” name=”s” />

<input type="submit" class="search-submit" value="” />

Once you have created your post-type-archive-{post-type}.php file, WordPress will automatically use it when displaying search results for that post type.

Creating a Custom Search Form for a Specific Taxonomy

If you want to create a custom search form for a specific taxonomy, you can use the taxonomy_archive_title() function. This function allows you to specify a custom search template file for a specific taxonomy.

All you need to do is create a file called taxonomy-archive-{taxonomy}.php in your theme’s directory. You can then put your custom search form code in this file.

Here is a basic search form that you can use:

<form role="search" method="get" class="search-form" action="”>

<input type="search" class="search-field" placeholder="” value=”” name=”s” />

<input type="submit" class="search-submit" value="” />

Once you have created your taxonomy-archive-{taxonomy}.php file, WordPress will automatically use it when displaying search results for that taxonomy.

Conclusion

In this article, we have shown you how to create custom search forms in WordPress themes. If you have any questions, please let us know in the comments.

When you are happy with the new content, please copy and paste the text into the existing article and update the word count at the bottom of the article.

Creating Custom Search Forms in WordPress Themes

WordPress provides a number of ways to create and display custom search forms. In this article, we will show you how to create a custom search form and display it in your WordPress theme.

We will assume that you are familiar with the basics of creating WordPress themes and have a basic understanding of HTML and CSS.

The first thing you need to do is create a file called searchform.php in your theme’s directory.

In this file, you will need to add the following code:

<form role="search" method="get" class="search-form" action="”>

<input type="search" class="search-field" placeholder="” value=”” name=”s” />

<input type="submit" class="search-submit" value="” />

This code will create a basic search form with a text input field and a submit button.

Next, you need to add the following code to your theme’s functions.php file:

function my_search_form( $form ) {

$form = ‘

‘;

return $form;

}

add_filter( ‘get_search_form’, ‘my_search_form’ );

This code will override the default WordPress search form with your custom search form.

Now that you have created your custom search form, you can style it using CSS.

The following CSS code will style the search input field:

input[type=”search”] {

border: 1px solid #ddd;

border-radius: 4px;

-webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,.1);

box-shadow: inset 0 1px 2px rgba(0,0,0,.1);

width: 50%;

padding: 6px 8px;

-webkit-transition: width .2s ease-in-out;

-o-transition: width .2s ease-in-out;

transition: width .2s ease-in-out;

}

input[type=”search”]:focus {

width: 80%;

}

The following CSS code will style the search submit button:

input[type=”submit”] {

border: 1px solid #0073aa;

border-radius: 4px;

color: #fff;

background-color: #00a0d2;

text-decoration: none;

-webkit-box-shadow: inset 0 1px 0 rgba(120,200,230,.5),0 1px 0 rgba(0,0,0,.15);

box-shadow: inset 0 1px 0 rgba(120,200,230,.5),0 1px 0 rgba(0,0,0,.15);

padding: 9px 18px;

}

input[type=”submit”]:hover {

background-color: #0087bd;

border-color: #0073aa;

-webkit-box-shadow: inset 0 1px 0 rgba(120,200,230,.6);

box-shadow: inset 0 1px 0 rgba(120,200,230,.6);

color: #fff;

}

You can also style the search form using the WordPress Theme Customizer. Simply go to Appearance ยป Customize and click on the ‘Search Form’ tab.

Here you can change the search input field and submit button colors, background color, and border radius.

Once you are done styling your search form, you can add it to your WordPress theme.

There are two ways to do this.

You can either add the search form to your theme’s header.php file or you can use the WordPress function get_search_form().

If you want to add the search form to your header.php file, you will need to use the following code:

If you want to add the search form to a specific location in your WordPress theme, you can use the following code:

This code will output the search form wherever you place it in your WordPress theme.

Word count: 542