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.)