How to rotate adsense ads based on author name

So do you want to know how to add adsense to your wordpress blog based on the author of the post?

Share adsense with authors based on author name.

Share adsense with authors based on author name.

Adding adsense based on user name or author is easy with a little PHP magic.
Using this code your revenue sharing arrangement will work out so the writers AdSense code will be used only on the articles with which they write. Revenue generated from articles that the authors have written is all applied to the users adsense ID. The actual java script that google AdSense produces when you generate an ad is what is used within the HTML produced buy the wordpress loop.

I like this form of revenue sharing because the AdSense revenue is based on pageviews and clicks for each author, the writer is getting exactly what he or she earned based on what they have written and the traffic it brings to your WordPress blog.

In order to add the adsense based on the author of the post you’ll need each author to sign up for adsense and email you a copy of a ad they generated for what ever size box (ad unit) you’re defaulting for your blog.

So lets say your authors email you this adsense code:

<script type=”text/javascript”><!–
google_ad_client = “pub-1345304564559123“;
/* 336×280, created 7/31/09 */
google_ad_slot = “1237752456“;
google_ad_width = 336;
google_ad_height = 280;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>

The adsense revenue sharing script will be used to replace the “google ad client” and “google ad slot” based on author user name.

So you need to copy the “google ad client” ID and the “google ad slot” ID – above they are “pub-1345304564559123” and “1237752456

Now insert all those google ID’s as seen below into the php script highlighted below as a quote.

You want to insert the entire below code snippet into your single.php file where ever you want the google adsense rotation code to show up. Usually if you want the adsense ad unit to show up at the bottom of the post just copy paste the below code directly above

<?php the_tags(‘<span id=”tags”><strong>’.__(‘Tagged as:’,).’</strong> ‘, ‘, ‘, ‘</span>’); ?>

or above

<?php comments_template(); ?>

Single.php is located in wp-content/wp-themes/”your-theme”/single.php

In order to get your adsense code to rotate based on author you need to replace the “green” item below with your Author name as seen in the wordpress admin panel.

Replace the “red” item below with your Author’s google pub-ID that they sent you via email.

Replace the “pink” item below with your Author’s google ad slot ID that they sent you via email.

<?php function get_ad_client($author) {
if($author == ‘username1‘)
return ‘pub-userspubIDhere‘;
elseif ($author==’username2‘)
return ‘pub-userspubIDhere‘;
else // default, no author found
return ‘pub-DefaultPubIDhere‘;
} ?>
<?php function get_ad_slot($author) {
if($author == ‘username1‘)
return ‘put-ad-slot-number-here‘;
elseif ($author==’username2‘)
return ‘put-ad-slot-number-here‘;
else // default, no author found
return ‘put-ad-slot-number-here‘;

} ?>
<script type=”text/javascript”><!–
google_ad_client = “<?php echo get_ad_client(get_the_author());?>”;
/* 336×280, top fantasy football */
google_ad_slot = “<?php echo get_ad_slot(get_the_author());?>”;
google_ad_width = 336;
google_ad_height = 280;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script>
<br><br>

The final results will look like this – use this code if you’re pasting it into your single.php file


&lt;!--
google_ad_client = &quot;";
/* BLOG 336 BIG BOX */
google_ad_slot = "";
google_ad_width = 336;
google_ad_height = 280;
//--&gt;

That’s all folks! You now will have google adsense show up on your post page in wordpress based on the Author name of the user. The author name is set in the admin panel for each user name. Let me know if you have issues or questions.

  • Trackback are closed
  • Comments (2)
    • Tito
    • November 14th, 2009

    Hello,
    When I tried to implement this code, I received the following error, just in the first line:

    parse error, unexpected T_STRING

  1. This is often easily fixable, as the problem is often simple. The problem stems form the syntax of your code.

    Paste the code as you used it on your site. the entire single.php would be nice.