web analytics

Google Adsense with WordPress Search

on February 11th, 2006 | Filed under Asides

For techies out there that use WordPress and Google Adsense, I just spent a long time figuring out something I heard about at acmetech.com.

The idea, as I understand it, is if someone searches your blog for something, but does not return any results, then you can display a little Google Search box with their search word(s) inside whose action searches Google using your Adsense account number so that you get advertising credit for any click throughs.

I didn’t immediately understand from the acmetech site where to insert their code snippit, and also the text didn’t have the right coding (the apostrophies and quotation marks especially weren’t compatible).
So if you want to try this out, edit in your default template the index.php file (which inside the WP control panal-Presentation-Theme Editor they call it “Main Template”).

Then here is the text code snippit from acmetech.com that I reformatted.

Right-click the above link to save the file to your desktop, open it in WordPad, copy and paste the text into your default template’s index.php file between
<?php _e('Sorry, no posts matched your criteria.'); ?>


< ?php endif; ?>

You may also notice if you test this out on my website, that the url now says optoblog.com/search/yoursearchterm instead of optoblog.com/index.php?s=yoursearchterm. They call it a “cruft free” URL, and I did it by following the directions at the WordPress codex site.


2 Responses to “Google Adsense with WordPress Search”

  1. You’ve become quite the WordPress guru, Dave. 🙂

    Nice hack – I implemented it. Is it supposed to auto-populate the search box with the search term? Because it’s not doing that for me.


  2. I think I figured it out. When I ask IE to download the file, it tries to save it as an htm document. Even if I tell it to save as a txt file, it still makes the apostraphe different from what I had to use.
    The code that makes it autopopulate is:
    input type =….. value=”… php echo $_REQUEST[\’s’];?…
    The apostrophes are wrong. In Wordpad, change them to the straight up and down apostrophes instead of the slanted apostrophes.
    Let me know if this helps.