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.


Suggested reading:


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

Comments:

11 Comments

  1. Vicky
    Dec 12th, 2011

    how do i delete built in search gadget plz help???
    you can see my blog template...... mobilemillion.blogspot.com

  2. Tegar
    May 5th, 2009

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

    • Shockwave
      Mar 9th, 2011

      The easiest way to add a search engine to your site is to use this code. Just plop it in your website.

      This will search the whole internet (in another window)

      <!--

      -->

      If you need code to search your whole website, or you want it to open the search results in the same window, as your search bar, I would need more information to build the code. Its pretty easy. Just email me at: [email]shockwave13@gmail.com[/email] and I will help you build the code.

      Cheers

  3. 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?

  4. 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.

  5. 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.

  6. Vladimir
    Apr 15th, 2009

    There is an option to turn off ads right there.

  7. 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?

  8. 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.

  9. 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).

  10. 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 *

*
*

Subscribe without commenting

About

vladimir prelovac Vladimir Prelovac is CEO of Prelovac Media, a computer engineer by profession and an adventurer by state of mind. more +


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

Services

Manage multiple WordPress sites

Built for WordPress enthusiasts, ManageWP helps you manage all your WordPress sites from one central location.

Books

WordPress Plugin Devleopment Book Read my book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon.