So do you want to know how to add adsense to your wordpress blog based on the author of the post?
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 what the author (or you) put in their profile.
So you need to copy the “google ad client” ID and the “google ad slot” ID – above they are “pub-1345304564559123” and “1237752456”
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 the auto needs to add their google_ad_slot and google_ad_client to their profile.
Add this to your functions.php
[sourcecode language=”plain”]
<!–?php remove_filter(‘pre_user_description’, ‘wp_filter_kses’); ?–>
<!–?php function my_new_contactmethods( $contactmethods ) { $contactmethods[‘googleadclient’] = ‘googleadclient’; // Add googleadslot336 $contactmethods[‘googleadslot336’] = ‘googleadslot336’; // Add googleadslot $contactmethods[‘googleadslot468’] = ‘googleadslot468’; return $contactmethods; } add_filter(‘user_contactmethods’,’my_new_contactmethods’,10,1); ?–>
[/sourcecode]
Add this to single.php
[sourcecode language=”plain”]
<script type="text/javascript">// <![CDATA[
google_ad_client = "<?php echo the_author_meta(‘googleadclient’);?>";
google_ad_slot = "<?php echo the_author_meta(‘googleadslot468’);?>";
google_ad_width = 468;
google_ad_height = 60;
// ]]></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// <![CDATA[
// ]]></script>
[/sourcecode]
Or, if you prefer you can have two adsense units. Each google adsense unit with be displayed based on elapsed time. One adsense unit for webmaster and the other for the author. The ad unit for the author will show up for 45 days (or whatever number of days/week/months you specify) and then the other one after that forever. We control how long the authors ad unit is displayed with the strtotime function as seen below.
[sourcecode language=”plain”]
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!– FK 468 –>
<ins class="adsbygoogle"
style="display:inline-block;width:468px;height:60px"
data-ad-client="<?php
if (strtotime($post->post_date) > strtotime(‘-45 days’))
{
echo the_author_meta(‘googleadclient’);
}
else
{
echo "ca-pub-40xxxxxxxxxxxx64";
}
?>"
data-ad-slot="<?php
if (strtotime($post->post_date) > strtotime(‘-45 days’))
{
echo the_author_meta(‘googleadslot468’);
}
else
{
echo "5xxxxxxxx1";
}
?>"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
[/sourcecode]
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.
Tito says
Hello,
When I tried to implement this code, I received the following error, just in the first line:
parse error, unexpected T_STRING
Kilroy says
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.