Blog Tip: Don't Display Google AdSense Within Sponsored Posts On Your WordPress Blog

There's a reason I inserted the Google AdSense code manually into my WordPress theme. If I were to use a plugin, I wouldn't be able to do what I'm about to explain. Of course, I didn't know that at the time but I knew I might want to do something like this in the future.

As I stated in my article, Blog Tip: Comply With Google AdSense Restrictions On Your WordPress Index Page, editing the relevant part of the theme is easy. Before I started writing sponsored posts, the complete code I used to display the Google AdSense ads on my index page was:

<?php
$gcount++; // google post counter
if ($gadv < 3) { // how many ads to display?
if ($gcount%1 == 0) { // display ad after how many posts?
$gadv++; // count number of advertisements
print '<br />
<script type="text/javascript"><!–
google_ad_client = "pub-8324670490712090";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468×60_as";
google_ad_type = "text_image";
google_ad_channel = "";
google_color_border = "ffffff";
google_color_bg = "ffffff";
google_color_link = "4592b7";
google_color_text = "769d1a";
google_color_url = "4592b7";
//–></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
}
}
?>

The AdSense code was copied exactly as provided by Google to my single post and static page templates.

While I was reading the guidelines for several sponsored post services, some of them required that no other ads were to be shown within the sponsored posts. It wouldn't be a problem with the static pages, but the AdSense code in both my index page and my single post page templates would have to be augmented with PHP code to prevent the ads from displaying on sponsored posts.

I decided to use a unique sentence that the PHP code could trigger on. It could have been anything that would never be a part of the sponsored post text or links. I had to use a built-in WordPress function to retrieve the post content and parse it before that part of the template retrieved the content so that the condition would tell the rest of the PHP code to skip it. Once I figured that out, I just needed to modify the code to include the function and the condition.

On the next line after the "<?php" opening tag, I inserted:

$content = get_the_content();

Then I modified the existing condition of:

if ($gcount%1 == 0) { // display ad after how many posts?

To read:

if ($gcount%1 == 0 and stristr($content, 'This is a sponsored post.') === FALSE) { // display ad after how many posts?

This solved the problem for the index page template and had the benefit of displaying the next ad after the sponsored post. Changing the code for the single page template was even easier. First, I had to insert this before the AdSense code:

<?php
$content = get_the_content();
if (stristr($content, 'This is a sponsored post.') === FALSE) {
print '

Second, I had to add this after the AdSense code:

';
}
?>

As you can see, there is no AdSense ad in this article because the sentence of "This is a sponsored post." appears above (and now here).

(Update 2007-08-11: I now use the Shylock Adsense Plugin to do the dirty work for me. I made one change to the plugin to make it work with certain kinds of posts but other than that, it does the job.)

Similar Posts:

11 Comments

  1. Tim says:

    I'm finding these Wordpress tips fun to read, but really confusing. I have been working on moving to a Wordpress driven site, and I find code really intimidating. Everyone's advice is to go it on your own, but Typepad lets me whip up some fast sh*t without knowing much. Almost rethinking my move plans, except I do want the freedom. Of course,If I were rich I could just pay someone to whip up some sh*t :mrgreen:

  2. [...] Blog Tip: Don't Display Google AdSense Within Sponsored Posts On Your WordPress Blog by RT Cunningham [...]

  3. Abhinav Sood says:

    whoa! Wise And Intelligent RT.

    I at my collge now. Just abt yesterday I came to know that laptops were mandatory here .. and the whole campus is wi-fi enabled .. I will get back to blogging asap

  4. Ozh says:

    get the plugin "Who Sees Ads" and never bother with those dirty theme hacks to conditionally display some ads :)

  5. [...] Thanks to one of the simplest, yet most useful tips that I 've ever come across, RT at Untwisted Vortex has the answer to this problem: Don't display Google Adsense within sponsored posts on your WordPress blog. [...]

  6. [...] lazy? I give up – why? …  Blog-Op: Here's a really good AdSense tip courtesy RT at Untwisted Vortex … Chris Garrett on New Media: Chris wants to know what kind of blogger you are … [...]

  7. [...] Blog Tip: Don't Display Google AdSense Within Sponsored Posts On Your WordPress Blog by RT Cunningham [...]

  8. onlinebisnes says:

    Hi there,
    Its a great post on guide to google adsense placement.
    But for the same result I think you can try mightyadsense wordpress plugin.
    With this plugin you can set how the adsense ads will be place on static page, single and index page.

    onlinebisnes's last blog post..Yahoo! Final decision On Microsoft Offer – Bid Rejected!

  9. Alvin says:

    Hey, great tip and great site. I'm making a serious go at making some money by blogging and appreciate tips like you offer.

    Alvin's last blog post..A must watch video – Gary Vaynerchuck at Strategic Profits Live 08

  10. Great post! Thanks for sharing all these insights, tips and resources.I remember clicking that now – to tell the truth – I think I am so conditioned to just clicking away TOS without even reading them, google could sneak anything through on me.

Leave a Reply

CommentLuv Enabled

This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.

Anti-Spam Protection by WP-SpamFree