Join Amazon Student FREE Two-Day Shipping for College Students!

Add Google AdSense Ads Code to WordPress Post After n Characters

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.

# 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"
(adsbygoogle = window.adsbygoogle || []).push({});
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.

Leave a Reply

Your email address will not be published. Required fields are marked *


Your browser is out-of-date. Upgrade your browser today! Internet Explorer | FireFox | Chrome