WordPress and adsense implementation

After working out where I should add ads on my web page, as described in wordpress and adsense this layout needed implementation. To keep everything maintainable I looked for some widget that I could use. Simply copying Google supplied code into relevant places can be messy for upgrades. Also, if I need to change something I must modify code again. Naturally I can not remember where I did what after a couple of weeks, not to speak about months or even years. But this plugin should also generate code as close to Google code as possible. Relevant excerpt from Google code snippet looks like this:



google_ad_client = "ca-pub-XX";
google_ad_slot = "XX";
google_ad_width = 300;
google_ad_height = 250;

First I tried Adsense Insert. Setting it up was simple but I could not find a way to specify google_ad_slot. Maybe I missed something but I did not have a good feeling for that plugin.  I picked a much simpler widget – Amikelive Google Adsense Widget instead. Using this widget is simple enough.

Adds in post body are a different challenge.  One choice is to use Ad Hoc Widgets. The theme I use, Suffusion, has them. Short code inside posts can be used to add those widgets. This approach is the least code intensive but also the hardest in my opinion.  I can not possibly remember to add some specific code to every post I create.

Another way is to modify single post template – single.php. Template defines how a post is displayed for users. As template file is a new file I can hope that I am able to only copy some files over after upgrade so there should be no major management issues. There is only one problem. I would like some posts to look a bit different. I use NextGEN Gallery to manage photo galleries.  Post template that did work well for regular posts did not look acceptable when used with galleries.  Another plugin helps here –  Custom Post Templates. With this plugin I can have different template for different posts. Now all that is needed is creating those templates.

By default post is displayed using single post template or single.php. For my installation and suffusion theme it is at


Generating another template starts with copying this file.

For this theme to have a names some lines must be added right at top:

Template Name Posts: Theme name goes here

Adding a single ad at the beginning of a post some more code is needed, just before


This is a rectangle ad which gets wrapped by text:

<div style="display:block;float:left;margin: 5px;">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-XX";
google_ad_slot = "YY";
google_ad_width = 300;
google_ad_height = 250;
<script type="text/javascript"

Similarly add for the bottom of the text is added after content. Also Google adsense suggested adding +1 button. I placed it  also after content.

Final file is available here 2post.php.txt

For gallery type post I just omitted wrapping for the first ad. Please remember to replace XX and YY with relevant Google codes if someone uses this template.

Leave a comment

Your email address will not be published. Required fields are marked *