Add Google AdSense Ads Code to WordPress Post After n Characters

Are you making money with Google Ads as what you expected? Adding Ads at the beginning or at the end of the post will not gives give you improvement on the clicking numbers. However, inject Ads into the post contents after words can boost your CTR rate significantly. Add the code below to your WordPress theme’s functions.php is all you have to do (NO PLUGINS NEEDED!). You will need to replace the Ads code to your own Google Adsense Ads code. You may tweak the number to adjust where you want the Ads appear (after how many characters). The example below will inject the Ads to all posts with at least 600 characters. The Ads will be add after the 590th character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Add Google ads after n characters

function inject_ad_text_after_n_chars($content) {
// only inject google ads if post is longer than 600 characters
$enable_length = 600;
// insert after the 590th character
$after_character = 590;
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 = '<p>
<!-- Google Ads Code -->
<ins class="adsbygoogle"
style="display:inline-block;width:234px;height:60px"
data-ad-client="ca-pub-6914615778368190"
data-ad-slot="6282177961"></ins>
<script>
(adsbygoogle = window.adsbygoogle []).push({});
</script>
</p>
';
array_splice($after_content, 1, 0, $text);
$after_content = implode('', $after_content);
return $before_content . $after_content;
}
else {
return $content;
}
}
//add filter to WordPress with priority 13
add_filter('the_content', 'inject_ad_text_after_n_chars',13);

You should test and adjust enable_length and after_character for best performance.

Google Voice + OBi202 = Free home phone service Still need DOS? Create a Bootable MS-DOS / FreeDOS USB Drive

Comments