Hacking Wordpress - Integrate Google Adsense for Search

by @ 9:50 pm on December 1, 2007.  Filed under WordPress

I did some hacking with Word Press today to replace its built-in search with Google Adsense Search. It was fairly easy, and I’ll explain how in this post.

First you’ll need to setup your Adsense account and get your search code. During the setup process you will be asked an important set of questions which controls how the search results are displayed. By default, Google opens a separate window to display your search results. This resulted in an ugly format, and lacked my WordPress theme. Choose the options as shown in the window below:

Adsense For Search Options

This should result in Google providing you to pieces of code, the Search Box code, and Search Results code. Use the Search box code in your leftcolumn.php theme file to replace the search box that WordPress defines.
I made some changes to Google’s generated code to make it fit my left column, and to make sure it posted to the index.php. Google uses a lot of hidden form fields to define the search results. We will use one of these hidden fields to tell our theme index.php file to display search results instead of the blog posts.

Here’s the portion of my hacked theme index.php that displays Google’s Adsense-enabled search:

At the top of my index.php, before the blog post code:

<div class=”centreblock”>
<?php if(isset($_GET['client'])) { ?>

<!– Google Search Result Snippet Begins –>
<div id=”googleSearchUnitIframe”>&tl;/div>

<script type=”text/javascript”>
var googleSearchIframeName = ‘googleSearchUnitIframe’;
var googleSearchFrameWidth = 700;
var googleSearchFrameborder = 0 ;
var googleSearchDomain = ‘www.google.com’;
</script>
<script type=”text/javascript”
src=”http://www.google.com/afsonline/show_afs_search.js”>
</script>
<!– Google Search Result Snippet Ends –>
<?php } else {?>

Now, further down the file, you will see this:

<?php
include_once(’leftcolumn.php’);
include_once(’rightcolumn.php’);
?>
</div> <!– centreblock –>

We need to add the following line:

<?php } // end google search if ?>
<?php
include_once(’leftcolumn.php’);
include_once(’rightcolumn.php’);
?>
</div> <!– centreblock –>

Of course, depending on your WordPress theme, your actual code might be different. I’m trying to think of other cool Googlish hacks I can do to WordPress, got any ideas?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

One Response to “Hacking Wordpress - Integrate Google Adsense for Search”

  1. Pit Boss Says:

    An after thought on this post. The theme I’m using for Webpit didn’t have the necessary support for Wordpress widgets. Since posting this, I’ve patched the theme to include the required widget functions. You could of course use the Widget manager to create a text widget and paste your adsense code. However, it might not appear the way you expect it. Through trial and error, and maybe the original approach I took, you could add your adsense search to your Wordpress site.

Post a Comment

Please click here to login to post a comment.

web·pit n. a place of discovery; a repository of information; where coders come for enlightenment; a programmers diary.

internal links:

categories:

search:

archives:

December 2007
M T W T F S S
    Jan »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

other:

All content Copyright (c) 2008, Mark Williamson