• Home
  • Systems
  • Networking
  • WordPress
  • Web
  • Geekery

Analysis and Review

How to Insert / Add Adsense into WordPress Post After n Words

July 29, 2013 by Kurt Turner

How to add Adsense to your wordpress post / articles after 100 words, or 1000 characters.

With the below script added to your functions.php you will insert a google adsense unit of your choice after the first 1000 character. It will insert after the first break in a paragraph.

 
[sourcecode language=”plain”]
function inject_ad_text_after_n_chars($content) {
// only do this if post is longer than 1000 characters
$enable_length = 1500;
// insert after the first </p> after 500 characters
$after_character = 1000;
if (is_single() && strlen($content) > $enable_length) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode(‘</p>’, $after_content);
$text = ‘
<br />
<script type="text/javascript"><!–
google_ad_client = "ca-pub-4092621159897364";
/* FK 468 */
google_ad_slot = "5827162911";
google_ad_width = 468;
google_ad_height = 60;
//–>
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br />
‘;
array_splice($after_content, 1, 0, $text);
$after_content = implode(‘</p>’, $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
add_filter(‘the_content’, ‘inject_ad_text_after_n_chars’);
[/sourcecode]

Filed Under: Wordpress

Latest and Greatest

Apache vs Nginx for WordPress

Certbot Lets Encrypt And WordPress MU Multi-Site

How To Find and Replace Multiple File in MS Word

How To Get Stretch Res on Windows 10 AMD Radeon For Game Play

This is the ASUS Chromebox we've been running for 2 years as our "brain" for our home grown digital display kiosk.

DIY Digital Signage Bulletin Board or Kiosk

© Copyright Analysis And Review · All Rights Reserved ·