Replace WordPress Search With Google Search

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Finally got to replace the “weak and inefficient” default search in WordPress, with blazing fast and powerful Google search. There are more advantages to it, than just the speed and convenience. You might end up generating some revenue on the side too. Even though you would need to get your hands dirty with php codes, the steps involved are easy and manageable for a complete php novice like me. There is just one requirement, you need a Google AdSense account.

Lets start with it then:

1. Log in to your AdSense account and click on Adsense Setup and then AdSense for Search.

2. Start by selecting Only sites I select for the Search Type field. In your Selected Sites field, type the URL to your blog, you can enter multiple sites if you want to search through multiple blogs you have.

3. You should enter the keywords that are  representative of your website for Optional Keywords, these will gear the ads displayed toward your blog subject matter, but these are not necessarily determined by these entries.

4. For the remaining fields you can choose to continue with the default entries. Move ahead by hitting Continue.

5. Here you optimize the look and feel of your search form, you might want to modify the search box field width at this point. Continue to the next step.

6. Next step is an important one, here we choose where to display your search results. In order to make the search results appear within your own site, choose “open results within my own site” and specify the URL. For Hackourlives, I went ahead with “”. This is obviously not part of the standard WordPress installation, and needs to be created. We’re going to create this file for you in a moment, the approach is that described here, with a couple of our own customizations.

7. Before moving to the next step, choose the location for Ad placement (keep your site layout and template in mind) and also modify the width of search results to meet you template requirements. You may also modify the Ad formating.

8. In the next step, name your search engine (for your own reference) and accept the Google’s terms of use to get the code.

9. Google would give you two chunks of code. One is for the actual search form and the other is for your search results page. Include the search form HTML wherever you want it to appear in your blog theme (likely either the sidebar or the header). You can use CSS to customize the appearance of the search form to match your site as closely as possible.

I went ahead and replaced the call to current search form with Google’s search box code as shown below. I found and replaced the following line,

<div class="search"><?php include (TEMPLATEPATH . '/searchform.php'); ?></div>

with this

<div class="search">

10. Now lets create a file called searchresults.php. The code that I used for this file is given below. You will need to paste in your results code for search result output that Google gave you, modify as needed to suite your particular theme, and upload to your blog’s root directory (not the theme root directory). The code is included below

//Include current WordPress Theme Header etc.
function maketitle() {
return " | Search Results";
//Check if we’re wrapping the WP Theme
//Get Theme settings.
$themes = get_themes();
$current_theme = get_current_theme();
$current_template_dir = $themes[$current_theme]['Template Dir'];
$current_stylesheet_dir = $themes[$current_theme]['Stylesheet Dir'];
//Initialize the WP class to be able to get the header
//Set status to 200 to override the 404 set by WordPress
//Include the WP Header
<?php get_sidebar(); ?>
</div><!--end post area single-->
<?php include ('path_to_second_sidebar/sidebar2.php'); ?>

The code above has two calls to sidebar, to maintain my template’s requirement, you might have to tweak these calls to suit your needs, other than that this should work well out of the box. You should now have Google custom search up and running on your website.

You might also like

Replace URL in WordPress Posts, Images and Links If you migrate...
How to Redirect / Forward Entire WordPress Blog Hackourlives...
WordPress Blog Post by Post Redirection! You recently...
Best Featured Posts / Content Slideshow Plugin for WordPress While there...

About the Author

has written 346 posts on this blog.

Meditating towards his "nerdvana", lifehacker is a teacher by the day and a nerdy blogger by the night. He lives to learn and yearns to learn living.

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!


Copyright © 2017 Hack Your Life!. All rights reserved.
Proudly powered by WordPress. Developed by Deluxe Themes