Adsense Code within Content

There are plenty of WordPress plugins that manage ad placement. Here’s a couple of ideas that will insert ad code (or anything else) after a defined number of paragraphs within your post.

The first snippet of code will insert adsense or other code after the first, second or any other paragraph. I use this same code on this blog. It’ll insert an ad block within the post as you will see below.

1. Open single.php in your wordpress blog themes folder.
2. search for the code: <?php the_content(); ?>
3. Replace the above code with the following:

<?php
$paragraphAfter= 1; // display after the number paragraph
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i  ) {
if ($i == $paragraphAfter) { ?>
<div>
Your add code here
</div>
<?php
}
echo $content[$i] . "</p>";
} ?>

Replace Your add code here with whatever content it is that you want to display. Replace the value in $paragraphAfter= 1 with the paragraph number it is that you want to display your content.

Ruud Hein has written a nice little hack that will insert code after the n’th paragraph. It comes as a plugin so you won’t have to update code as you upgrade WordPress.

<?php
/*
Plugin Name: Adsense in Content
Description: Insert an add after the Xth paragraph. Configure in the plugin file.
Version: 1.1
Author: Ruud Hein
*/

add_filter('the_content', 'article_ad');

function article_ad($post_content) {
   global $wp_query, $post;

   if (!is_single() ) return $post_content;

   // After which line number to insert the ad
   $afterParagraph = 1;

   // Adsense code
   $adsense = 'Adsense code in here';

   // $matches is an array of paragraph number and [1] character length
   preg_match_all('/(?:\r\n|\r|\n)/', $post_content, $matches, PREG_OFFSET_CAPTURE );
   $insert_at = $matches[0][$afterParagraph][1];
   return substr($post_content,0,$insert_at). $adsense . substr($post_content,$insert_at,strlen($post_content));
}
?>

It’s not the easiest plugin to use and its use is rather unpredictable. It uses line breaks as markers (the times you hit enter in your content) so is prone to all kinds of errors.

I’ve just sent out an email to the mailing list with a link to a small pack containing code that will assist you in displaying code in various ways on your page; such as having it placed so text wraps around it. I’ll be making this code available for general download shortly.

You can download the plugin below.


Download: Adsense in Content
Description: Ruud’s code will insert adsense or other code after a defined number of paragraphs.
Author:Ruud Hein
Category: PHP code
Date: February 8, 2010

First Name:
Your Email Address:
 


If you liked this article, you may also like:

  1. Add WordPress tags within your post
  2. [Shortcode] Insert Google Adsense with shortcode
  3. Add Anything to the End of WordPress Post Content
  4. Find and Replace Words in a WordPress Post
  5. Add author details after each post in a WP RSS feed
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Trackbacks

Speak Your Mind

*