How to add manually adsense code to your wordpress blog in blog index and single page posts

Thursday, 7th April 2011

I’ve recently realized that the Easy Adsenser plugin which I used to place google adsense advertisements on my blog, is probably stealing some portion of my clicks.

There were some fraud reports on wordpress.org by people who have found out the author of Easy Adsenser rips clicks, by showing sometimes his own ad code even if the plugin is configured to not grant any clicks as a donation to the plugin author.
I don’t know how true this story is and I don’t have the time to observe the whole plugin code to say for sure if the rumors about clicks stealing are true.

However as I’m paying my internet access (that guarantees) by blog to stay online with some adsense advertisements and the adsense revenues are either equal to my internet tax or a bit higher (depending on the month), it’s quite unpleasent to hear someone is stealing from the ads clicks which still generate very low revenue.

Thus I took the time to read some blog posts online which gave me some hints on how can I directly place the google adsense advertisement code into the theme template files

My goal was to place one google adsense ad to appear right after the title of each article and one to appear as a vertical bar in the end of my sidebar.

In this article in short I’ll explain how I achieved this banner placement via the default wordpress template which obviously I use on my blog.

Let’s start:

1. Add adsense to the index page of the blog

Edit your blog/wp-content/themes/default/index.php file

Therein find the code:

<div id="content" class="narrowcolumn" role="main">

and right after this line put the following php code:

<?php
$postnum = 1;
$showadsense1 = 1;
?>

This code is necessery to assure the adsense code only appears on the first blog post from the blog index page

2. Find the code:

<small><?php the_time('F jS, Y') ?> <!-- by

Immediately after the code place the php code:

<?php if ($postnum == $showadsense1) {
echo '<div class="adsense" style="float:right;margin:12px;">;paste here your adsense code ...</div>';
} ?>

<?php $postnum++; ?>

Now with this changes, 1 adsense advertisements should start appearing right after your first and only on your blog post, next step is to place manually one more vertical adsense banner.

2. Place adsense vertical bannre in wordpress blog sidebar

Login with admin user to wordpress and navigate to:

Appearance -> Widgets

Among the available widgets you will notice the widget called Text click over: Add to add this widget to the list of widgets to appear on your blog sidebar.

Afterwards look up over the Sidebar list of widgets find the newly added Text widget and click over Edit to modify it’s content.

Further on put a Title for the widget or choose to leave the title field as blank if you don’t want a name to appear.
On the next textbox just paste your adsense code and you’re done. A simple refresh of your wordpress blog index page should show you a vertical banner with your adsense code.
! Note that if you have recently issued the adsense code it will take about 10-20 minutes until the banner starts showing up.

Until now wordpress is configured to show adsense adverts on the blog main page, as a next step we need to place the same adsense adverts to appear whether a single blog post is reviewed (opened).

Place an adsense advertisements to single posts opened

For that purpose it’s necessery to edit the file single.php it’s again located in blog/wp-content/themes/default

Once again you will first need to find the code:

if (have_posts())

Put the code after the end of the line on a new line:

<?php
// below code is for adsense
$postnum = 1;
$showadsense1 = 1;
?>

Next lookup in the file for the code:

<h2><?php the_title(); ?></h2>

On a new line after it place:

<?php if ($postnum == $showadsense1) { echo '<div class="adsense" style="float:right;margin:12px;"><script type="text/javascript"> place here your adsense code </div>';
} ?>

<?php $postnum++; ?>

That’s all now the adsense advertisements will be also showing on the single blog posts reviews found via some search engine (google, yahoo etc.).

Hope this article will be helpful to somebody, if so drop me a thanks line in comments 😉

Share this on:

Download PDFDownload PDF

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

2 Responses to “How to add manually adsense code to your wordpress blog in blog index and single page posts”

  1. Terrence Kotch says:
    Firefox 3.8 Firefox 3.8 Ubuntu 9.25 Ubuntu 9.25
    Mozilla/5.0 (X11; U; Linux i686; it-IT; rv:1.9.0.2) Gecko/2008092313 Ubuntu/9.25 (jaunty) Firefox/3.8

    Thanks Man this really Helped Me To Improve My Adsense Ads CTR As Well As CPC And I am Started Getting 50% More Clicks on my Blog Than Earlier

    View CommentView Comment
  2. Jessica - Noticias Virales says:
    Internet Explorer 7.0 Internet Explorer 7.0 Windows XP Windows XP
    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)

    Hi! thank you very much for the information, is what I was looking, has served me well!, Thanks and kisses!

    View CommentView Comment

Leave a Reply

CommentLuv badge