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]