Skip to content


Google Adsense with WordPress Search

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.'); ?>

and

< ?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.
Enjoy!

Posted in Asides.


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Josh Bancroft says

    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.

    Thanks!

  2. David Langford says

    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.
    -DJL



Some HTML is OK

or, reply to this post via trackback.

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. If you would like a custom picture by your name, then go to gravatar.com to link your e-mail address to your picture.



Bad Behavior has blocked 242 access attempts in the last 7 days.