Using Google Adsense for Search in WordPress

I wanted to use Google “AdSense for Search” as the default search engine on my wordpress site. After experimenting with several of the wordpress plugins to set up the Google Search, I just wasn’t satisfied with any of them. So I asked myself: “Why couldn’t I do something like I’d been doing with plain old websites”? It turns out that I could and I ended up with something that seems pretty simple, to me.

My reservations with the existing plugins had to do with modifying code in the theme. Although, I’m more than capable to modify the theme, I did not want to remember to do so every time I changed or upgraded my theme.

When you setup Google “AdSense for Search” on a plain old website, you:

  • create a search results page that contains the “Search Results Code” JavaScript that is generated by Google.
  • create a search button on the pages of your website, using some more Google generated JavaScript code.
    This is done by placing the “Search Box Code” JavaScript that is generated by Google on the pages of your website where you want the search button to appear.

Much to my surprise, this turned out to have a fairly direct translation to wordpress. It seemed pretty simple to do.

First, I setup my Google “AdSense for Search” which created 2 JavaScript code blocks: “Search Box Code” and “Search Results Code”

Next, I created a search results page for my site. Making sure that my permalink was in the format of http://mysite/search and then in source view I added  the Google generated JavaScript code for the “Search Results Code” from my “AdSense for Search”. This will look something like:

<script type="text/javascript"><!--mce:0--></script>
<script src="" type="text/javascript"><!--mce:1--></script>

Last, I removed the standard wordpress “Search” widget. I created a new Text widget and placed the Google generated javascript “Search Box Code” from my “AdSense for Search” into the text of the new widget. This will look something like:

<form id="cse-search-box" action="http://mysite/search/">
<input name="cx" type="hidden" value="partner-pub-xxxxxxxxxxxxxxx:yyyyyyyyy" />
<input name="cof" type="hidden" value="FORID:10" />
<input name="ie" type="hidden" value="ISO-8859-1" />
<input name="q" size="32" type="text" />
<input name="sa" type="submit" value="Search" /></div>
<script src=";lang=en" type="text/javascript"><!--mce:2--></script>

Then I made my new search widget which was named “Text” the first listed widget.

This seems to work pretty well.

Maybe in a future release wordpress will let me give user controlled names to my text widgets. I currently have 3 widgets named Text :-)


Be Sociable, Share!
This entry was posted in google, wordpress and tagged , . Bookmark the permalink.