How To Replace Your WordPress Search With Google

Google is the undisputed king of search. They do it well. What many bloggers may not be aware of is that you can use Google as your site’s main search engine. You don’t have to use your blog engine’s built in search.

There are two really nice benefits to using Google for your site search engine:

  1. It’s Google! That means you have full power of Google search on your blog.
  2. You can use Adsense for your search results and make some money on your site searches. And the CPM is usually VERY high with Adsense in this case because the ads are super well targeted.

So the question is: how do you do it? Well, I’ll show you. :)

  1. You need an Adsense account. So, if you’re not already signed up as an Adsense publisher, do so now.
  2. In your Adsense account, click on “Adsense Setup”. Then click on “Adsense for Search”.
  3. Click the “Get Started” button. You will enter the wizard for creating your search form.
  4. For “Search Type”, select “Only sites I select”. In your “Selected Sites” box, type the URL to your blog. You can enter multiple sites if you wish.
  5. Enter some “optional keywords” which will gear the ads displayed toward your blog subject matter.
  6. You can leave all other fields as default. Continue to the next step.
  7. Choose the look and feel of your search form. Continue to the next step.
  8. Next, you can choose where you want your search results to appear. In order to make the search results appear within your own site, choose “open results within my own site” and specify the URL. For PCMech, I entered “http://www.pcmech.com/searchresults.php”. This is obviously not part of the WordPress installation, but that’s OK. We’re going to create this file for you in a moment.
  9. Choose the ad position and color palette and then continue to the next step
  10. Assign a name to the search engine. This is for your own internal purposes later. Then hit the “Save and Get Code” button.
  11. You’re going to get two snippets 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.
  12. Now you need to create a file called searchresults.php. The code I use for this file is below. You will need to paste in your results code that Google gave you, modify as needed to suite your particular theme, and upload to your blog’s root directory.


<?php
unset($_SERVER['PATH_INFO']);
//Include current WordPress Theme Header etc.
require(‘./wp-config.php’);
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
wp();
//Set status to 200 to override the 404 set by WordPress
status_header(200);
//Include the WP Header
add_filter(“wp_title”,’maketitle’);
get_header();
?>
[ PASTE YOUR SEARCH RESULTS CODE FROM GOOGLE HERE ]
<?php get_sidebar(); ?>
<?php
get_footer();
?>

This file will use your blog’s active theme, however you may need to do some modifications to suit your blog. You might also need to go back and define the width of the search results to fit your blog.

When you’re done, you will have a Google powered search engine within your blog.

NOTE: The only caveat to keep in mind here is that your posts will not appear in your search engine until Google indexes you. However, if you post often and use the XML Sitemaps plug-in, you should be fine.

Blog Transformation Challenge

Like what you read?

... then take the next step. Register for the 30 Day Blog Transformation Challenge. TRANSFORM your blog by perfecting your writing, optimizing your design, and learning how to optimize the marketing funnel.
Click to Learn More ... or...

  • nickaster

    Hey, this is great thanks! I can get the thing to work, but only if I totally ignore the theme and just paste raw google results on an unformatted page – see if for yourself here.

    Works, but ugly. In this case, I just ignored your template all together and only pasted the code google gave me. If I use your template, I get a blank page for my results. I wonder if it's related to the location of search.php – in the root, whereas the other wordpress files are in /wordpress. However, messing with the required URL above doesn't help.

    I have no idea how to work with PHP, so I'm kinda screwed unless someone knows something obvious to look for?

  • nickaster

    Weird. This worked for a while then conked out…. what would cause that? http://www.triplepundit.com – disqus keeps killing my links, otherwise you could see…

  • nickaster

    Weird. This worked for a while then conked out…. what would cause that? http://www.triplepundit.com – disqus keeps killing my links, otherwise you could see…

  • http://bychai.net byChai

    Thanks David for the excellent info. I used it on my site and had the php errors on line 4 and 6 at first. Thanks to the commenters for listing the fixes. Also, I installed the php file in the theme folder instead of the root, which was another problem. Re-read your instructions and all is working like a charm.

  • http://www.zephyrww.com/m Michal

    Hi David,
    I recently came across this post at looking for a way to embed Google search in my blog. So first off – thanks! your guide has helped a lot! :-)
    If I might suggest a way to actually add the search result page as a part of the theme itself:
    Before actually getting the code from Google:
    Add a new file under your theme folder.
    Call it “Google_search_results.php”
    At the very beginning of the file, between the php brackets, add this comment:
    /*
    * Template Name: Google search results
    */
    Then copy and paste into the file, the get_header(), get_sidebar() and get_footer() code, according to your theme
    Save the file

    In the admin area create a new page
    Call it “Google search results”
    Set its visibility to private
    Select its template to “Google search result”
    Copy the URL of the page from the “Permalink” line.
    This URL should be registered under google Adsense.

    Tehn, go to your Google adsense account and create the code, registering the url of the new page as the search result page location.

    Good luck!
    Michal

  • http://www.bloggingshiksha.com/2011/09/how-to-make-money-with-adsense-prime-source-of-online-money/ How to make money with AdSense

    No a days many people do this and it has worked them to start making money…..

  • http://www.sysdigg.com/ Marvin J

    Is there a wordpress plugin, that can simply plugged into wordpress ?

  • http://searchenginemagic.org/ Search Engine Magic

    The SEO process can enhance your online visibility tremendously by getting you top rankings on search engines. With the right technique, you can get your sales chart moving upwards.