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;
<div style="margin-top:-10px;margin-bottom:16px;margin-left:-10px;">
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.
Hey RT,
The timing of your guide is perfect. I was just thinking of adding it to my blog.
Cheers
James
Great guide – but I need to ask a stupid question. Why use Google Search instead of the default WordPress one? Is it just better? I’m sure there’s a good reason, so I’m hoping you can enlighten me.
I know you can use Adsense with Google Search, but I assume that’s not the reason, now you’ve given Adsense up. :smile:
Google Search is a better search mechanism. Try hunting for a specific phrase using the default search. You get every article with that phrase without any context. It’s not easy to show the difference until you start getting more than 30 results.
Some day, I may decide to build a better mousetrap and write a search mechanism that doesn’t use Google but is far beyond what WordPress search uses. Don’t hold your breath.
Thanks for the explanation. I only have 22 posts at present, so I don’t get many results for anything I search on.
Thanks for this!
But when you have a moment, can you stop by and tell me how I can position my search so it’s not SMACK on the top of the page? HA!
I just stopped by your blog and left you a message.
Hey, thanks for the tip. I haven’t even thought of adding Google Search to my blog which is stupid because I could be getting some adsense revenue. :shock:
the tips you gave was surely exhaustive, thanks for taking the time to explain the whole thing… but for blog owners with very little knowledge on writing codes, its a bit hard to follow your steps (you lost me in the part about page.php)… its really hard to absorb everything you said in there, about the “part of default “page.php” you need above” and “below”..
i think it has to do with how you presented the whole process. if you’re open to suggestions, instead of explaining it in paragraph form, maybe you could just simplify it further like, step 1, 2, etc, with the shortest sentences as possible for each step… i dont mind if it will take twenty steps or more, so long as each step is short and easy to follow…
having said that, i’m looking forward to another version of this topic, this time hopefully easier to follow for “real wordpress dummies/newbies” like me.