One Way To Handle AdSense Ads on a Multi Author WordPress Blog

Post written by: Mike

Paul and I decided from the start that we wanted to monetize the site but didn’t want to spend a ton of time optimizing and tweaking and monitoring. Just something simple that could be a little carrot to keep us entertained and maybe pay the hosting costs.

We currently just have the AdSense text links just under the header of all pages and the AdSense medium rectangle before the comments on the single posts.

The tricky part was trying to figure out how to get ads to rotate the way we wanted. We decided that some kind of random 50/50 impressions split would be fine for now and I couldn’t find a WordPress plugin that would do it the way I wanted without needing to put some php directly in the AdSense code to figure out which publisher ID to use which made me a little squeamish.

In the end, I added a bit of code and figured I should share the info in case some other blog authors want to do something similar.

First off, because I don’t know how to get the variables to pass from one php file to another and since Google does not allow two publisher IDs on one page, all of the code that gets displayed for any given pageview is in one php file.

For the main page and the archives, we are just using the text ad under the header. So, in the archive.php and index.php we have this after the get_header and before the content divs start:

<? srand(time());
$random = (rand()%2);
if($random == 0)
{
?>
[My AdSense code]
<? }
elseif($random == 1)
{
?>
[Paul's AdSense code]
<?}
?>

The first two lines just get a random number that is either 0 or 1.  Then if it is 0, my ad shows and if it is 1, Paul’s does.

Nice and simple.

For the single.php, it starts out with the same code after the get_header. Then after the content and before the comments, there is another if/elseif.  This second time on the page, we don’t get the random number because we need to make sure that both ads on the single post get assigned to the same publisher ID.

There might be WordPress plugins out there that handle this with fancier reporting in WordPress or greater control in where the ads show up, but so far this has been working pretty well for us.  If we find out that the random zeros show up more than the random ones, we can just flip who gets displayed for each number until it starts to even out.

2 comments ↓

#1 beagoodmom on 08.03.07 at 8:43 am

Hello- I visited your site, to prove that I love you. I won’t be visiting again, unless you start talking in english.
Love, your wife.

#2 Paul on 08.03.07 at 9:45 am

Ha ha ha! I’ll have to show this comment to my wife, as she can probably relate.

Leave a Comment