Multiple Adsense Between Paragraphs in WordPress Blog Posts

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.

13 Comments

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

  2. 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?

  3. Thanks. This post was very helpful!

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

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

  6. And is it good to use between every paragraph Because there is limit to use adsense in one page …
    U can use only 4 units am i right

  7. Adsens code contain ” and ‘
    So
    echo’

    YOUR ADSENSE CODE HERE

    ’;
    }
    Is not absolutly correct ;)

  8. This is exactly what I’ve been looking for! Unfortunately I can’t get this to work in 2.6.3. I’m at the point where I’d be willing to hire someone to get this working for me. Any interest, Erik?

  9. CCF is given to you in the curve coding ..

  10. i didn’t understand.

Trackbacks/Pingbacks

  1. [...] 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 [...]

  2. [...] 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 [...]

  3. [...] 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. [...]

Leave a Comment