Oh hi Google, come on in!
As I’m sure most of you know, the WordPress search function is terrible; hell, even the developers know that WP search sucks. WP search returns results in reverse date order (rather than in order of relevancy), and it has a lot of trouble with phrases. After seeing Hung give it a spin at AnimeNano, I decided to give Google Search a try as well. After all, Google is all up in this blog’s business already. So here’s a little starter lesson on how to not only replace your WP search with Google Adsense search, but also make everything look nice and integrated.
Set Up Adsense for Search
The first thing you need is an Adsense account, which most webmasters should already have. Navigate to Adsense Setup >> Adsense for Search. Most of these options are self-explanatory, but make sure you select that you want the results to load on your own site. You’ll also want to make sure that the search results are the right width. Unfortunately, the smallest you can make this value is 795 pixels, so if you have a really narrow site, this isn’t going to work that well.
I already knew I was going to call my page 'Search'
I also set my search up to search on http://www.seaslugteam.com/*in order to only return results from SS!AB. If the reader wanted to search the whole web, he probably would have gone straight to Google instead of using my site’s search function. Once you’ve customized the various options, click Submit and get the 2 blocks of Google code. The first block is for the search field, while the second block returns the results of the search.
Create A Search Page
Next, you will need to add a template to your WP theme. There are tutorials out there for creating page templates, but chances are that your theme already has a few. The easiest way to make a custom WP page template is to go into your theme folder and copy and rename the page.php file. At the top of the page, place the following code:
<?php /* Template Name: NAME_OF_YOUR_TEMPLATE */ ?>
Once you upload this template into your theme’s folder, you’ll be able to select the template in your Edit Page screen (Attributes >> Template). You should give the new page a title (in this case Search, so that the URL will be http://www.seaslugteam.com/search/ as I told Google in the previous step), and also hide the Search page from displaying in the navigation menu (with this helpful plugin).
You should probably disallow comments and pings on this page, too
The two additional changes that you will make to this page template to differentiate it from a typical page are:
- Remove the sidebar: Find the code
<?php get_sidebar(); ?>or<?php include (TEMPLATEPATH . "/sidebar.php"); ?>and remove it. As mentioned before, the search results must be at least 795 pixels wide, so chances are you cannot fit both the search results and the sidebar. If your theme allows for you to have a 795 pixel body and a sidebar, you do not have to delete the code for the sidebar. - Paste in the Google code: I took the code that Google gave me in the second box (that returns search results) and pasted the block underneath
<?php the_content(); ?>. I also added a comment above the Google code. This means that the webpage will render just like a normal page on your blog, but with the search results right under your (empty) page content.
Replace the Search Widget
Go to your Widgets page, and remove the old WordPress search widget. Create a new text widget, and paste in the code from the first box that Google gave you (that displays the search box). Make sure to save your widget settings. You should now see the Google search box where your WP search box was.
Tweak Your Style Sheet
For this step, the Firefox plugin Firebug is invaluable. This plugin will help you figure out which id or class you need to change in your stylesheet to get everything looking just right. The two main changes you will probably have to make are to the width of your post area on your search page (because your stylesheet probably restricts this area, to give the sidebar some space), and to the padding on your widget. This step is not too difficult, but the settings differ from theme to theme. If you have to go back and tweak your Google settings at this point, remember to re-paste the new Google code into your template, because the width is hard-coded.
Notice that 910 width + 10 padding = 920 total pixels
In the Thematic theme, the id that we are interested in is called container. Since this id is used a lot, I decided to change <div id="container"> to <div id="container_search"> on my search.php template, so that I wouldn’t have to change any of the other theme files. I then added the following lines to my style.css file.
#container_search{ width:910px; padding-left: 10px; }
My container is set to 920 pixels, but I wanted to offset my search results by 10. Therefore, I told Google to return the results in a 910 pixel box, and set the padding on the left to 10. I also added a few pixels of padding to cse-search-box, to get my search box to display in line with my other widgets, but this is a matter of preference.
Put Ya Guns On!
It is critical that you watch this video before proceeding
Wrong Holic, but good try!
That’s it, you’re done! You can always go back and tweak the look a bit, but all search results should look as if they came from your blog. I’ve found Google’s search to be a lot better, and I prefer the format of Google’s results. As much as I expect WP to improve their search function, I can’t really see it coming close to what Google has. Plus, the chance at additional ad revenue, however small, is a nice bonus (although when I used “Welkin” as my test term the first day, Google kept returning ads for Belkin).
Update: I realized a week later that if you select “Top and Bottom” for the Ad Location, the minimum width drops to 500 pixels. If your main content area is wider than 500 pixels, you can leave the sidebar in and skip the CSS tweaks, making the process even easier.
Update 2: Also note that you can paste the code for the widget box (which displays the search bar) into your template above the code for the search results. This makes it more convenient for readers in case their first search needed to be corrected or refined. You can change the line that reads <input type="text" name="q" size="31" /> so that the size is larger and fills the width of the content area. I changed mine to size="120" and keep in mind that this is the length of the field in terms of character length and not pixels.


{ 19 comments… read them below or add one }
Very nice. Will be helpful when/if I eventually swap over to WP.
I should be able to try that next week once my domain’s AdSense account gets approved. (My first account on a different e-mail got disabled in October because I was being an idiot.)
Well, haha, the same happened to me, too, earlier
I thought the google results were kinda crappy, at least for the purpose of finding stuff on Anime Nano (and getting more $$$). I just switched back because it required more clicks the Google way.
I think the wordpress search is pretty good. What’s so bad about it?
First off, WP search is so bad that even WP creator Matt doesn’t use it on his site. Here are a few ways how WP search sucks.
Basically, the search is especially terrible if you have a lot of content-rich posts on your blog, and if anyone wants to perform a multi-word search. One of the reason AN’s results were not as good is that you were drawing from RSS snippets, and not full articles.
The note about low $$$ is true, but that wasn’t a major motivator in the switch. Getting some search tracking was nice, but not a major motivator either.
Default WordPress search is showing results in a some alien order, sometimes the results don’t even contain what you searched for!
I’m switching now. Thanks, Kabitzin.
Updated the tutorial to show that you can get down to 500 pixels width if you change the ad locations. I also noticed that the Google Search seems to search through comment content which I find very helpful.
Wow, I didn’t know that. Thanks, Kabitzin, that was uber-helpful, now I don’t need to think how to band my in-background-drawn sidebar! ^__^
I was playing around with the search, and I think it is alot better than it used to be (even though I don’t recall ever using it). I can now find all of Chris159159159′s comments =D
I’ve run into a problem wrt Hontou ni Taihen desu – after keying in the search terms, the page just reloads as the main page with no search results. I’m suspecting it’s because HNTD uses the ?page_id=N permalink structure…
Seems odd; did you use new code in both widget and search area for Stripey’s?
Yes, it was new code because I needed a different colour scheme (on top of the new page URL).
All the tutorials I’ve googled just assume that permalink is set to pretty rather than ugly so I haven’t encountered any other description/solution of this issue. (Assuming the permalink structure is the problem.)
It doesn’t seem like Google would be defeated by something like permalink structures. Maybe try completely recreating a new Google Search and re-pasting both pieces of code into Stripey’s site again. If it isn’t a coding error, I wonder if perhaps you can only have one Adsense search per account.
IIRC Google allows up to two search boxes even on the same page. I’ve checked the code against the one at HNSO (which works fine) and it compares ok.
I agree that it’s somewhat implausible that Google could be defeated by permalink structure but I’m wondering whether permalink is just the symptom rather than the cause. E.g. mod_rewrite is not activated for the default ugly permalinks…
Will you be implementing Google Search on kolaap?
Hmm, maybe I will try it out on one of my other sites and see if it works. .htaccess issues could be a plausible explanation as well.I just added the search to KOLAAP and it worked just fine
. Try editing the Adsense code to display search results on a new Google page (instead of in the site) to confirm that it is a problem with Stripey’s site and not with Adsense.
It works!! Thank you!!! *tears of joy*
I’ll consider this issue as resolved as I really don’t mess around with Stripey’s permalink structure. Gah, I still wish we hadn’t decided to leave it as default though.
BTW I added the lines to turn on mod_rewrite in HNTD’s htaccess file but it didn’t work either.
I accidentally set the search results for KOLAAP to display in Sea Slugs at first (and it worked), so it would be uber sneaky if you used Stripey’s search to get your site more hits
.
The Holy Hontounia Empire is an indivisible whole, capitulating only to a single throne!
Well, I do most of the backend maintenance anyway even though Stripey’s the engineer and I’m the one with the French fries degree. Go figure. Besides we share our (minuscule) AdSense revenue to pay for hosting equally – so we’d still get nothing in the end regardless of uber sneaky plots. orz
It doesnt work, the search function does not work on your post either and its not what people are looking for, thier is already a search for most themes we want to know how we can change the existing one we alreadyy have. :blank:
{ 2 trackbacks }