So, I knew I wanted to put Adsense ad blocks in the middle of WordPress posts. I knew I could do it with some simple hacks to WP, but I thought it should be a plugin. It turned out to be remarkably easy to do, and it turned into my first WP plugin. Of course, I “sampled” lots of code from pioneers who went before me.
Adsense — in case you don’t know, is Google’s little advertising engine for non-Google sites. You know when you see Google ads on a site somewhere, and it says “Ads by Gooooogle” or somesuch? That’s it.
Google has some rules about how you place Adsense code on your site, though, and how many Adsense blocks can appear on a page (two?). I haven’t tried to accomodate these rules in this plugin yet. I probably will have to do that down the road. Caveat emptor.
The plugin [download] is pretty straightforward. Put it in your wp-content/plugins directory, then go to your Plugins configuration page and click Activate.
You’ll need to customize the Adsense code in the plugin itself. Look in the adsense.php file. In the first function there’s a block that says “Begin Google Adsense code”. You want to replace the lines between that line and the one that says “End Google Adsense code” with your actual adsense code provided to you by Google. Otherwise, you’ll be showing ads for me.
Once you activate the plugin, you should notice a new button on your Quicktags toolbar. The new button is “Adsense”, and it works like “more”. Click it to insert the Adsense tag, and enjoy the newfound wealth.
Here’s an example:
[Edit: sometimes I see the ads, and sometimes I don’t. It’s just Google’s peskiness. You can view the source and see the ad code is there.]
Because of Google’s policy on multiple ad blocks per page, I recommend that you always place a “more” tag before the “adsense” tag. This way, users will only see the ad if they click the blog entry to see the full thing. Otherwise, you may have several uncontrolled entries on a page all showing the ads. And Google may frown mightily upon you.