Shared Google Reader Items #11

Show AdSense to Google Search visitors in WordPress

I put AdSense ads on my WordPress plugin pages since the donation button wasn’t exactly raking in the dough. I inserted my AdSense code straight into these pages’ content. Looking through some of my stats I noticed a few of my posts got lots of hits from Google Search that would be good for ads. I didn’t want to show ads to my friends and normal readers so I went looking for a simple solution.

I borrowed some of the code used in this post, http://www.scratch99.com/2008/01/shylock-adsense-plugin-hack-to-avoid-adsense-smart-pricing/. One template function checks the referrer, if it returns true then you display your AdSense code in your post template, single.php in my case. This goes in your functions.php:

function scratch99_fromasearchengine(){
$ref = $_SERVER['HTTP_REFERER'];
$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
foreach ($SE as $source) {
if (strpos($ref,$source)!==false) return true;
}
return false;
}

and this goes in your post template where you want the ads to be displayed:

<?php if (function_exists('scratch99_fromasearchengine')) {
if (scratch99_fromasearchengine()) { ?>
INSERT YOUR ADSENSE CODE HERE
<?php } } ?>

And that’s it. Visitors coming in from Google and some other search engines will have whatever ad you want displayed. There are plugins that will do this all for you, but this is simple enough to build straight into your theme. The only downside is if you are using caching, but odds are only your older posts are going to be getting hits from Google. If a page happened to get cached with the AdSense code inserted then everyone will see the ads. Not really a big deal to me, but just something to be aware of.

Blog CSS makeover

Did a little work on my blog’s theme to bring it up to speed with WordPress 2.7. Been using the same theme with a few modifications since WP 1.5 so it was time for a little refresh. List of changes:

  • New font and font sizes.
  • Reset all padding and margins. This made it easy to get consistent spacing across the entire site using margins only.
  • New comments output with gravatar support. Using the new wp_list_comments function with a custom callback to separate trackbacks.
  • Moved header text down so the tails of the Y’s go into the background of the main column, that’s the extent of my creativity.
  • Category and tag pages only show excerpts now.
  • Edited robots.txt and sitemap.xml to avoid Google indexing duplicate content on archive pages.
  • Increased total container width to 895px, my stats show only about 1.2% of people who visit my blog are on 800 x 600 so there’s no point in designing for them anymore.
  • Added code to show Google AdSense to visitors coming straight from a Google search. Did that about a month ago, I’ve made $7 in  February so far.

New Nikon 35mm f/1.8 AF-S DX lens

Nikon just announced the AF-S DX NIKKOR 35mm f/1.8G this week. I promptly placed a preorder at Amazon.

If you own a D40/D40x/D60 then this little $200 lens should be on your list. Previously the 50mm f/1.8 was the only affordable way to get a fast prime from Nikon, but without the AF-S you don’t get autofocus on these cheaper cameras. Also, on the DX cameras, the 50mm becomes 75mm which is a bit long. The 35mm becomes a 52.5mm which is much closer to the traditional “normal” 50mm on a film camera.

This lens focuses closer than the 50mm (less than 1ft vs about 1.5ft) and should be noticeably wider. Looking forward to its release in March.

Shared Google Reader Items #10