Displaying ads after first paragraph with WordPress or after any paragraph you wish

I recently was faced with the task of displaying ads after the first paragraph and the 5th paragraph. I found a very useful script by Travis over on WPSmith that came very close to what I needed. I made a few modifications to fit my needs and figured I share this. Put the below code into functions.php of your theme:

Code to displaying ads after first paragraph with WordPress

add_filter( 'the_content', 'ads_after_paragraphs');
function ads_after_paragraphs( $content ) {

	//prevent filtering of certain pages
	if (is_page(array('about-us', 'contact-us', 'contact', 'privacy-policy')))
		return $content;

	//Set ads to be shown after certain paragraphs, here after 2nd and 5th
	$adsafterparagraph = array(1,4);

	global $post;

	//your ad js directly from Google Adsense here, wrapped in a div
	 $ad = '<div class="inline-ad">'.YOUR_AD_JS_CODE_HERE.'</div>';

    $content_expl = explode("</p>", $content);
    for ($i = 0; $i <count($content_expl); $i++ ) {
		if (in_array($i, $adsafterparagraph)){
			$content_expl[$i] = $content_expl[$i].$ad;
			}
    } 

	return implode("</p>", $content_expl);
}

5) I try and prevent filtering certain pages, you may use wp functions like is_page, is_home to achieve your goal. I took some slugs away here as an example.

9) Define after what paragraphs you want to add the ads code (or really any code). Note that the array indexes start at 0, so you have to adjust for this.

16) Explode the content. Essentially builds an array using the closing paragraph tags as separators.

17-21) Run loop and add in code at end of paragraph. Btw if you wanted the ads at the beginning of the paragraph, just change line 19 to:

$content_expl[$i] = $ad.$content_expl[$i];

23) Glue the array back together into a string and return it. Very important to return code in filters to preserve compatibility with other plugins.

Some css to display the ads properly

And the little bit of css I use with it. Put it into style.css:

.inline-ad{ margin: 5px; float: left;}
h1, h2, h3, h4{clear: both;}

I do suggest you clear the titles, otherwise it can happen that they get floated and look pretty ugly…

A few notes on why I changed parts of the code from Travis:

  • My code works with a regular WordPress install. Travis uses a genesis hook.
  • Filtering the content makes sure it is compatible with other plugins that filter content. It is important that you return content you filter.
  • This code supports multiple ads of the same type by setting the paragraph display numbers into an array.

Either way, big shout out to Travis for his code!

Does this code help you? Do you have any questions or did you run into a problem? Please share, I am happy to help.

The ideal season to determination heartiness problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile disfunction is more common among older men, that doesn’t some you simply have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile malfunction. You may have heard about “canadian pharmacy online cialis“. More information about the problem available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a dispenser that they are sure to take with your other remedies.

The Top 8 WordPress Developer Tools from 5 years experience
(Hint: They cut down my development time in less than half!)

Get the Top Tools Now!

Comments

  1. Bob says

    Hello Michael,

    Quick question on your code. What if i want to place an ad before the first paragraph…(right under the featured image…but before the 1st paragraph). What code could I use?

    Thanks,

    Bob

Leave a Reply

Your email address will not be published. Required fields are marked *