Home Displaying ads after first paragraph with WordPress or after any paragraph you wish
Post
Cancel

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<pre lang="php">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:

1
<pre lang="php">$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:

1
2
<pre lang="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.

This post is licensed under CC BY 4.0 by the author.
Trending Tags