I’ve added Google Search to two different blogs now, so I think I know what I’m doing. If you read my instructions, you can add Google Search to your blog to replace the default search in most themes.
Obviously, the first thing to do is to set up the search form through the Google AdSense site. Make sure the option to have it open on your site is selected. Once you have it looking the way you want it, copy the search form script to a text file called “searchform.php” and copy the results script to a text file called “googlesearch.php“.
You want to save your old search form as a backup. I usually just tack on “.bac” to the end of the filename because my text editor automatically appends “.bak” when saving a new copy. The “.bak” file changes every time I save the file I’m working on and I want my original, pristine copy to remain untouched.
Before you copy anything to your server, you need to make sure the files are set up properly. The “searchform.php” file will work as is for some themes but not for all of them. Some themes require a “<div>” tag at the beginning and a “</div>” closing tag at the end (like the ProSense theme). The “googlesearch.php” file will not work until you add theme code from your default “page.php” file to it.
The part of the default “page.php” file that you need above the script for the “googlesearch.php” file is every line prior to line that contains “the_content()” function except for a possible “<div>” tag. It may not hurt to leave it there. If you remove it, make sure you remove the corresponding “</div>” closing tag. I used “<div align=”center”> in that spot on mine.
The part of the default “page.php” file that you need below the script for the “googlesearch.php” file is every line after “the content()” function and the “link_pages()” function (if it exists).
Replace all the comments (if any) at the top of the “googlesearch.php” file with just this:
/* Template Name: google search */
Copy the completed “googlesearch.php” file to the theme directory of your blog, but don’t copy the “searchform.php” file to the server yet. You need the results template in place in order to create a page using the template. Go to your admin options and write a page. Call it “Google Search” and select the “google search” page template in the right column. Save it as is.
You don’t want the results page to appear in any page menus, so go back in to “manage pages” and get the ID number for the page you just created. Locate the function “wp_list_pages” in your “sidebar.php” file. Within the single quotes, insert “exclude=X&” (without quotes) before the rest of the code. I have the function in both two theme files for this blog and only in one file for the other blog.
Copy the “searchform.php” to the theme directory for you blog, bring your blog up in your browser and test the search. If you did everything right and it works but the results are too big for the page, you can reduce the “var googleSearchFrameWidth” number in your “googlesearch.php” file. I had to drop it to 540 on this blog and 480 on the other blog.
The “searchform.php” may need to be adjusted with CSS properties. On one site, I replaced the first “<div>” with;
I could’ve added the CSS properties to my main style sheet, but this keeps everything isolated to a single file. The search box was too big for my sidebar, so I made it smaller in the “searchform.php” file (from 31 to 25). Your theme may or may not require you to make it smaller.
That’s all there is to it. As I stated in the beginning, I’ve done it for two blogs now and each only took a few minutes once I figured out how to do it. That part took a lot longer. Hopefully, I’ll save you the trouble of figuring it out for yourself.