5Wordpress LogoAdsense ads above the last paragraph of a post

You may have noticed that we’ve tweaked how we dis­play our adsense ads.  We wanted some small text-only ads near the bot­tom of each art­icle, which would be added auto­mat­ic­ally.  This took a little bit of tinker­ing, but even­tu­ally we developed a solu­tion which works well and does­n’t seem to knock page pro­cessing times much.  Simply add the fol­low­ing code to your theme’s functions.php (don’t for­get to change your adsense IDs) 

Updated 12-Nov-2017 as the code was­n’t work­ing cor­rectly as pre­vi­ously dis­played. I’ve also added 2 oth­er func­tions that I use, one to insert adverts via short­code, and anoth­er to add an advert where the more is removed when a post is shown in full

/************************************************************************\
* Adsense Short code for insertion of 2 small ads anywhere via shortcode *
\************************************************************************/
function Google_Adsense( $atts, $content = null ) {
   return '<div class="a" style="text-align: center;"><div id="google-1"></div><p style="clear: both"></p></div>';
}
add_shortcode('google_adsense', 'Google_Adsense');




/******************************************\
* Show 2 small adsense ads at the more tag *
\******************************************/
add_filter('the_content', 'adsense_added_at_more_tag');

// This function replace your more tag with your adsense codes.
function adsense_added_at_more_tag($text) {
if( is_single() ) :
$ads_text = '<div id="google" class="a" style="text-align: center;"><div id="google-1"></div><p style="clear: both"></p></div>';
$pos1 = strpos($text, '<span id="more-');
//"
if($pos1 === FALSE) $text = $ads_text . $text;
else
{
$pos2 = strpos($text, '</span>', $pos1);
$text1 = substr($text, 0, $pos1);
$text2 = substr($text, $pos2+7);
$text = $text1 . $ads_text . $text2;
}
endif;
return $text;
}




/*******************************************************\
* Show single large adsense ad above the last paragraph *
\*******************************************************/
add_filter('the_content', 'gads_added_above_last_para');

function gads_added_above_last_para($ytext) {
if( is_single() ) :
$yads_text = '<div class="a" style="text-align: center;"><div id="google-2"></div><p style="clear: both"></p></div>';
if($ypos1 = strrpos($ytext, '</p>')){
$ytext1 = substr($ytext, 0, ($ypos1 + 4));
$ytext2 = substr($ytext, ($ypos1 + 4));
$ytext = $ytext1 . $yads_text . $ytext2;
}
endif;
return $ytext;
}

Leave a Reply

5 Comments

SJSarah Jones

I don’t know but your code was not work­ing in my case. So, I replaced it with oth­er code. If any­one else is facing the same prob­lem, then you can use the below code.

func­tion insert_ad_block( $text ) {

if ( is_single() ) :

$ads_text = ‘My Ad Code Here’;
$split_by = “n”;
$insert_after = 2; //number of paragraphs

// make array of paragraphs
$para­graphs = explode( $split_by, $text);

// if array ele­ments are less than $insert_after set the insert point at the end
$len = count( $para­graphs );
if ( $len < $insert_after ) $insert_after = $len;

// insert $ads_text into the array at the spe­cified point
array_splice( $para­graphs, $insert_after, 0, $ads_text );

// loop through array and build string for output
foreach( $para­graphs as $para­graph ) {
$new_text .= $para­graph;
}

return $new_text;

endif;

return $text;

}
add_filter(‘the_content’, ‘insert_ad_block’);

Reply
MSMatthew Snider

Is there a way to do this say after the 2nd para­graph instead of before the last one?

Reply
JSJon Scaife

Abso­lutely. You would just need to tweak the code slightly. I’m using strrpos which finds the last occur­rence of <p>
If you wanted to find the second occur­rence of <p> replace line 13 from the code above with…

if($pos1 = strpos($text, '<p>', strpos($text, '<p>') + 3)){
Reply
CChris

Great work man. Thanks a lot. Do you have any idea how to place a hori­zont­al line above and below the ad unit?

Reply
JSJon Scaife

Sure. Add a couple of <hr />‘s — 1 at the start of the value of $ads_text and the oth­er at the end.
E.g. replace

$ads_text = '<div class="a" style="text-align: center;"><script type="text/javascript"><!--
            google_ad_client = "ca-pub-0754629982287605";
            /* DMH-PostsMini */            google_ad_slot = "5138459326";
            google_ad_width = 468;
            google_ad_height = 15;
            //-->
            </script>
            <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script></div>';

With

$ads_text = '<hr /><div class="a" style="text-align: center;"><script type="text/javascript"><!--
            google_ad_client = "ca-pub-0754629982287605";
            /* DMH-PostsMini */            google_ad_slot = "5138459326";
            google_ad_width = 468;
            google_ad_height = 15;
            //-->
            </script>
            <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js">
            </script></div><hr />';
Reply