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!
2 Replies to “Restore default WordPress search”
Thanks for this very simple explanation (I needed it simple!). I have tried the search.php page with some success, but I have a couple of questions or issues if you have time please.
1. The title and excerpt appear, but there is no link. I managed to work this one out, adding to your code slightly:
2. You code only searches for posts, and I wanted pages too. I haven’t worked out how to do that yet. Do you have any code that would work please? I presume it would be quite different code for pages as for posts?
3. I presume the search lists the results in reverse chronological order, which seems fine to me for posts, but wouldn’t work for pages.
4. Is there any limit to the number of posts that are shown?
Thanks for any ideas you have about this.
1. It seems my code has been stripped out of comment #1. All I did was use “the_permalink: and wrapped it in text to make it use the title as link text.
2. Correction: it appears I was wrong. The search does give both pages and posts, which is great! Presumably they are both in reverse chronological order. I wonder if there is a way to put pages first (since they are more important than posts)?
Thanks again for this.