Free WordPress Tutorials

  • How to upload new Themes
  • How to setup a blog feed
  • How to Upgrade WordPress
  • How to install Google Analytics stats

Learn how to use WordPress here »

Premium WordPress Themes

Premium WordPress Themes

CLICK HERE TO FIND THE BEST WORDPRESS THEMES!
Get full support, great designs and more at iThemes!

Howto: Rotate Adsense for 2 Accounts

March 3, 2007

Since this site is co-operated by 2 individuals, it can be difficult to split ad profits evenly between myself and Cory. Don’t get me wrong, we have mutual trust for each other, but it would just be a hassle to have to make sure one of us splits up the revenue and sends it to the other person. To remedy this, I wrote a small PHP script to rotate our adsense code. Essentially, it randomly chooses an adsense block and displays it.

First, we have to get PHP to give us a random number. We’ll limit the options to just 1 and 2 since we are only rotating 2 adsense blocks.

[code]$rand = rand(1,2);[/code]

Then, we write the qualifier, an “if, then, else” statement.
[code]if ($rand == 1) //Comment: If the random output equals 1, then display Cory’s Code
print ‘YOUR CODE GOES BETWEEN THE SINGLE QUOTES’;[/code]

Yes, it is important to use single quotes. Just paste one of the two blocks of adsense code between the single quotes.

Next, we write the statement for the second block.

[code]elseif ($rand == 2) //Comment: If the random output equals 2, then display Nathan’s Code
print ‘YOUR CODE GOES HERE, LEAVE THE QUOTES THOUGH’;[/code]

Notice that I used “elseif”. Do this for all qualifier lines after the initial “if” statement.

Here’s what the final product should look like:

[code] $rand = rand(1,2);
if ($rand == 1) //Cory's Code
print 'YOUR CODE GOES HERE, LEAVE THE QUOTES THOUGH';
elseif ($rand == 2) //Nathan's code
print 'YOUR CODE GOES HERE, LEAVE THE QUOTES THOUGH';
?>[/code]

Save the file as adsense-rotate.php and upload it to your server. It doesn’t really matter where you put it, just make sure to use the correct path when calling it.

Finally, you’ll need to add a line to your template to call the file:

[code][/code]

Put that code in your template wherever you want your code to show up.

Notes:
If you want to rotate more than just 2 ad blocks, just change the second number in the randomizing line to the number of blocks you want to rotate. Then, just add a new “elseif” statement for the other blocks of code you want to rotate for.

Premium WordPress Themes

Get Email Updates on New Themes

Be the first to know when we release new themes
and get special discounts on premium themes!



RockinThemes uses the Essence Theme to run this blog, click here for more awesome WordPress themes!

Comments

3 Responses to “Howto: Rotate Adsense for 2 Accounts”

  1. jay on May 18th, 2007 12:00 am

    is this a 50/50 split ?
    if its choosing it at random wouldnt it be possible that one gets more than the other?

    thanks

    Jay

  2. Nathan Rice on May 18th, 2007 6:55 am

    jay,

    In the end, it does end up being even. in short bursts, it may be uneven, but it because of the random nature of the script, it does even out.

    Nathan

  3. Neale on July 12th, 2007 1:46 pm

    Thanks i have been searching for something exactly like this thanks

Got something to say?