How to Insert Adsense Ads Inside Blog Post Content (No Plugin)

Can you display Adsense ads inside blog posts without plugins? In-text ads are popular nowadays. There is no doubt that in-content ads blends better with content and encourages readers to click more, and the higher CTR can earn more money online. While it is easy to add Adsense ads below post titles or after post content, how can you add it inside articles?

So how can you add your Adsense like this?

adsense middle

Add Adsense Inside Post Content

Here is the code you need to add to the functions.php file of your WordPress theme. [Note it is a good idea to keep backup of functions.php file as any error can make your site unusable and you might need to restore the file via FTP. I use Filezilla for this.]

add_filter( 'the_content', 'qot' );
function qot( $content ) {
if( !is_singular() )
return $content;
$paragraphAfter = 5;
$content = explode ( "</p>", $content );
$new_content = '';
for ( $i = 0; $i < count ( $content ); $i ++ ) {
if ( $i == $paragraphAfter ) {
$new_content .= '<div class="middle-ads">';
$new_content .= 'ADSENSE CODE';
$new_content .= '</div>';
}
$new_content .= $content[$i] . "</p>";
}
return $new_content;
}

Insert Adsense Code explained

1. What this code does now is locate the paragraph after which you want to add the code. Right now we add the link code after the 5th paragraph. If you want the adds further down the page, you can change the value here from 5 to whatever you need.

$paragraphAfter = 5;

2. It is also important to locate the ads correctly. We have used the content-ads class here to style the ads. You will typically need to add margins, padding or float elements to correctly position the ads. For example, you can use

.content-ads {margin: 10px 0;}

3. Remember to replace the ADSENSE CODE with the code for your account, else this will not work and no ads will display.

4. If you only want to add the code to single posts, change the is_singular (which will add ads to both posts and pages) to is_single.

The credit for the original code goes to Brad Dalton who posted the code on Github. We have modified it a little to suit our needs.

If you liked this, you can also insert Adsense ads between blog posts on archive pages as well.

Adsense WordPress Adsense Plugins

Of course, there are very powerful Adsense plugins that can let you perform custom ad placements almost anywhere you want. But I prefer to add code to functions.php and keep it simple.

Many plugins are heavy to load, and indeed provide multiple options for power users, but many features provided by plugins might not be needed. But still if you want that, try Advanced Ads or Ad Inserter. They can precisely target where you want to place ads on your page and filter by page, post, or archives with many micro management options.

Share with friends

About the Author: P Chandra is editor of QOT, one of India's earliest tech bloggers since 2004. A tech enthusiast with expertise in coding, WordPress, web tools, SEO and DIY hacks.