Erik’s Blog: Board Shorts and Business Suits

Building Bussiness Systems from the Shores of Waikiki

Entries Comments



Multiple Adsense Between Paragraphs in Wordpress Blog Posts

5 September, 2007 (22:56) | Wordpress Tips | By: Erik

I’ve been a way for a while, trying to regain some focus in my life and thought I would return to this blog by providing some tips while using Wordpress. My most recent jump into playing with php and Wordpress is how to add Adsense code within blog posts in between paragraphs on every post.

I searched around and only found some plugins here and there that limit where the Adsense code can go. So I decided to build my own little php code. This should allow you to deal with the “” tag as well.

For this I’ve just dealt with the single.php file but you can put this wherever you’d like. Just remember the limits you have on the number of adsense blocks you can put in.

OK, so it’s really simple. Where you see <?php the_content(); ?>. Just replace that with:

<?php
$content = apply_filters(’the_content’, $post->post_content);
$save = explode(”</p>”, $content);
$count = 1;
foreach ($save as $a){
if ($count == 3){
echo’
<div style=”margin: 5px 0px 5px 0px; text-align: center;”>

YOUR ADSENSE CODE HERE

</div>’;
}
echo $a.”</p>”;
$count = $count + 1;
}
?>

It’s that simple! You can play around with the foreach and the if statements to insert your adsense between different paragraphs, multiple if statements allow for multiple inserts between multiple paragraphs. You can use if in_category to display ads a certain way if they’re in a certain wordpress category. The list is endless.

Now, for those of you who actually want to know how this works here goes:

The first line

$content = apply_filters(’the_content’, $post->post_content);

pulls your content into a string into the variable $content. This includes all code, html or otherwise. Don’t ask me why the apply_filters was needed. All I can tell you is that I wasn’t able to do it any other way.

Next we chop up the string into an array split up by </p> using the explode function.

$save = explode(”</p>”, $content);

As long as you are using paragraph tags in your posts (some people choose to take them out) then this will work. Each array value will be a separate paragraph.

Then we initialize a counter to keep track of how many paragraphs we echo (to come later),

$count = 1;

We then start our foreach loop to loop through each value in the array $save.

foreach ($save as $a){

Then we check to see what paragraph we’re on. Here is where you can play with it to echo your adsense code wherever you’d like. I have it displaying adsense after the third paragraph but you can display it wherever. Such as, if ( $count == 1 || $count == 3) would display before and after. You can use elseif to display different adsense layouts at different points in the post. Be creative. Notice that I’ve also decided to center my code using an inline style.


if ($count == 3){
echo’
<div style=”margin: 5px 0px 5px 0px; text-align: center;”>

YOUR ADSENSE CODE HERE

</div>’;
}

We then print our array value $a remembering to add a </p> at the end because php explode, explodes the array but removes the value we told php to explode from.

echo $a.”</p>”;

Finish that off with an increment of the $count variable and you’re done!

$count = $count + 1;
}
?>

This should give you the bare bones to insert adsense code into anywhere you’d like that you are displaying content. I use it only in single.php on my wordpress blog. You can see an example at my Hawaii Blog in this popular article about Dog the Bounty Hunter headed to Jail in Mexico.

Hope it helps some people out. Spread the word if you know anyone who can benefit from this.

Comments

Pingback from Adsense for Wordpress Posts, Between Paragraphs - Blog Tips
Time: September 6, 2007, 11:38 am

[...] then I think you may like this tutorial. I recently wrote the tutorial at my personal blog about adding adsense between paragraphs on your blog posts by using just [...]

Comment from Michael Samuel
Time: September 7, 2007, 1:41 am

Thanks for the nice tutorial! i will try this and give you my feedback….Blogger.com has also added a new feature that allows you to do the same. Thanks for the nice post. keep it up

Comment from Rishi
Time: October 11, 2007, 6:41 pm

Hey This Tip Is Really Amazing. I just added it to my blog with some changes. Any idea about how to do this in Drupal?

Comment from webabc
Time: October 20, 2007, 3:37 pm

Thanks. This post was very helpful!

Pingback from Adsense for Wordpress Posts, Between Paragraphs « Myemail’s Weblog
Time: January 30, 2008, 11:25 pm

[...] then I think you may like this tutorial. I recently wrote the tutorial at my personal blog about adding adsense between paragraphs on your blog posts by using just [...]

Comment from sheetal
Time: April 8, 2008, 11:21 pm

hi,
you said it was single.php file, but i couldn’t find

Pingback from SEO and Affiliate Marketing » Blog Archive » SEO Affiliate Marketing
Time: June 8, 2008, 7:25 am

[...] Multiple Adsense Between Paragraphs in Wordpress Blog Posts - Erik’s I’ve been a way for a while, trying to regain some focus in my life and thought I would return to this blog by providing some tips while using Wordpress. [...]

Comment from Erik
Time: June 30, 2008, 4:28 pm

Sorry sheetal about the late followup. Some themes don’t have single.php and you have to create it.

Easiest way to do that is to take your index.php file, save it to your desktop, change the name to single.php, upload that file to the theme, then presto, you now have a single.php file to play with that will control all single post views.

Write a comment