March 2018 is finally here and Google Site Search (GSS) is actually going away for good. While owners of larger websites have already happily settled into alternative premium services, some of us smaller guys have decided to retreat back to the comfort and predictability of WordPress’s built-in search engine. But, how do you restore the default WordPress search? Let’s take a peek under the hood and figure it out together.
Resetting the Search Form
This function can be placed into your theme file wherever you want the search form to appear.
This function will first attempt to load the file “searchform.php” but if it cannot find it in your main theme or child-theme, it will load the default WordPress search form. So, if you’ve customized or created the “searchform.php” file, renaming it (or deleting it) is one way to force your theme to load the default WordPress search form.
This will also cause any standard search widgets you have in your sidebars or footers to revert back to the default WordPress search form as well.
The default behavior of the standard WordPress search form will send your query to your homepage and attempt to display it there.
So the quickest route to get back to default is to:
- Rename or delete the file searchform.php in your child theme
- Add a Search widget into your sidebar OR edit your theme template to include the get_search_form(); function
- Run a search and see if you like the way your results display on your homepage.
The Search Results Page
What if the results look like rubbish? The search results are controlled by a file called “search.php” in your theme or child-theme. Take a look inside /wp-content/themes/your-child-theme/ or /wp-content/themes/your-parent-theme/ to see if there is a search.php file in there. If not, we’ll need to create one so we can improve the look of your search results page.
I’ll likely do a more intricate blog post about how to create a hyper-customized search results page, but for now, you want to look at your “page.php” file in your parent theme and use it as your guide to editing your “search.php” page. Here’s a quick start guide:
- Duplicate or create your search.php file into your child theme. (I like to name my old one search-bk.php just in case I need to refer to it later)
- Customize your new “search.php” file to beautify the display of your search results.
Want an example? Here is the one that I’ve created to use with the project I’m working on tonight:
<?php get_header(); ?> <?php if (have_posts()) : ?> <div id="content"> <h1>Search Results</h1> <?php while (have_posts()) : the_post(); ?> <div class="entry"> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> </div> <?php endwhile; ?> </div> <?php else : ?> <h2>No posts found.</h2> <?php endif; ?> <?php get_sidebar(); ?> <?php get_footer(); ?>
Create a Custom Search Form
Are you also less than happy with the way your search form looks in your theme’s template pages or sidebars? No worries. We can customize that as well.
If you want to recreate a custom form, we’ll need to create or edit a file in your child theme named “searchform.php”. Your code inside of “searchform.php” should look something like this:
<form action="/" method="get"> <label for="search">Search in <?php echo home_url( '/' ); ?></label> <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" /> <input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" /> </form>
No other headers on it are needed. It can just float all alone on the page without any html or php headers as it will be dropped into your pages as-is.
So, there you have it. This is the bare minimum of what you need to know to restore your website back to using the default WordPress search tool. There are lots of plugins and premium services you can use to enhance your search, but this at least gets you back to square one.
Please let me know if you have any questions or notice any typos/bugs in this post!