How to Add Google Custom Search to your Blog

Having played with this recently, here is a quick guide before I forget it. I had to use a little trick, I am not sure if there is an easier way to set it up.

All right, let's get started. We want to use Google Custom Search since your blog deserves better search than the one that comes with WordPress.

First go to Custom Search page and setup the new engine. Option worth noting is Advertising status where you can disable ads if you are non profit.

Once done click 'Code' Page, select 'Host results on your website', and iframe option. Enter your site URL.

This will give you two codes.

First one is the one that you need to put in place of your search form.  Find the form in your template, usually in sidebar.php or searchform.php.

Here comes the trick. You need to add the following code into the form:

<input type="hidden" name="s" value="Search" />

This is important because it triggers the WordPress search page (search.php).

Now open search.php and replace what you had before with the code in the second box from Google.

Basically you are done now.

You can do more thing. Add this to the top of your header.php:

<?php $s=$_REQUEST['q']; ?>

Now your theme will display the correct value for search term.

This is really a WP hack - I am not saying it's the  best or easiest way to do it.

You can even add the search button in your posts. Go ahead and try it out.

See also:


Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
leave a comment.

9 Comments

  1. Tegar
    May 5th, 2009

    oh my.. your article was really awesome... thanks yoo..

  2. CoolHappyGuy
    Apr 18th, 2009

    I know that you have already set this up for us with your Amazing Grace/Blue Grace themes.

    My question is how do we tweak the code to add AdSense for search?

  3. ignacio
    Apr 16th, 2009

    Done!

    I recommend anyone who does this, to go a step further and mix it with analytics. Google makes it really easy, just look for it.

  4. John Hoff - WpBlogHost
    Apr 15th, 2009

    I suppose you can, but it does say (for registered non-profits, universities, and government agencies only).

    Technically, I think you're suppose to pay a subscription fee to turn off ads.

  5. Vladimir
    Apr 15th, 2009

    There is an option to turn off ads right there.

  6. John Hoff - WpBlogHost
    Apr 15th, 2009

    The only issue I have with this tweak (the free one) is how Google places paid ads in the search. So if you have a product you're trying to sell, say web design, and someone goes to your search and types in "Web Design", they will see the results of your competitors before yours (in the paid search area at the top of the returned results).

    Not so ideal.

    But if you fork over the $100/yr. subscription for Google Search, then no worries, but is it work it?

  7. Bamarlay
    Apr 15th, 2009

    When I installed the google site search (from adsense) on my myanmar2day site, I tried to make it appeared within my site design. After a few trials and errors, I succeeded. I put the google ad search box in the side bar and direct it to open in the "search" page. Then, I put the google's search result code in the "search" page, and make it hidden in the header links. This solve the problem.

  8. Rarst
    Apr 14th, 2009

    I probably made it in even less proper way - threw out everything related to WP native search and sticked in CSE search form and templated page with CSE frame.

    Seems to work except Opera is having minor display error with iframe I failed to fix (so far).

  9. Chris Dornan
    Apr 14th, 2009

    Thanks Vlad--you are a treasure!

Have your say

Your email is never published nor shared. Required fields are marked *

*
*

This site rewards regular commentators with do-follows links to their site.

Subscribe without commenting

About Vladimir

vladimir prelovac Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.

"I would love to change the world, I just don't have the source code yet."

Books by Vladimir

WordPress Plugin Devleopment Book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon. Click the image for more information.

Consulting Services

Professional WordPress solutions based on custom developed plugins and themes

Expert on-site WordPress SEO consulting and an 'out-of-the-box thinking' approach to problems