You may have noticed that we’ve tweaked how we display our adsense ads. We wanted some small text-only ads near the bottom of each article, which would be added automatically. This took a little bit of tinkering, but eventually we developed a solution which works well and doesn’t seem to knock page processing times much. Simply add the following code to your theme’s functions.php (don’t forget to change your adsense IDs)
Updated 12-Nov-2017 as the code wasn’t working correctly as previously displayed. I’ve also added 2 other functions that I use, one to insert adverts via shortcode, and another 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; }
I don’t know but your code was not working in my case. So, I replaced it with other code. If anyone else is facing the same problem, then you can use the below code.
function 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
$paragraphs = explode( $split_by, $text);
// if array elements are less than $insert_after set the insert point at the end
$len = count( $paragraphs );
if ( $len < $insert_after ) $insert_after = $len;
// insert $ads_text into the array at the specified point
array_splice( $paragraphs, $insert_after, 0, $ads_text );
// loop through array and build string for output
foreach( $paragraphs as $paragraph ) {
$new_text .= $paragraph;
}
return $new_text;
endif;
return $text;
}
add_filter(‘the_content’, ‘insert_ad_block’);
Is there a way to do this say after the 2nd paragraph instead of before the last one?
Absolutely. You would just need to tweak the code slightly. I’m using
strrpos
which finds the last occurrence of<p>
If you wanted to find the second occurrence of
<p>
replace line 13 from the code above with…Great work man. Thanks a lot. Do you have any idea how to place a horizontal line above and below the ad unit?
Sure. Add a couple of <hr />‘s — 1 at the start of the value of $ads_text and the other at the end.
E.g. replace
With