You are currently viewing How to Insert Auto Repeating AdSense Ads Between Paragraphs Without Plugin

How to Insert Auto Repeating AdSense Ads Between Paragraphs Without Plugin

To increase revenue in AdSense, ads can be added inside content. Using Auto Ads in AdSense user can achieve this but not all of us like Auto Ads. Auto Ads is great for revenue but it also makes a website look bad by placing ads all over the site. Adverts can be inserted inside content between paragraphs in WordPress using plugins but again not all of us want to install more plugin. So I will show you how to add AdSense Ads which will auto repeat after some paragraph like AdSense Auto Ads.

There are plugins that allows to insert advert in different place in a page which makes it easier. But plugins may cost and you don’t want to use one more plugin. This ads code can be used with any WordPress theme, like Genesis, GeneratePress, Divi.

Are you using AMP? You may want to see –

This code inserts ads after third paragraph and then again after another third paragraph and again and again. Add this below code at last line inside functions.php of your theme.

You need to replace unit ad code with your ad unit code.

<?php //-------------------------------------------------------------- //-------------------------------------------------------------- //---------COPY CODE BELOW THIS LINE---------------------------- //Adding adsense ad unit inside the article //Insert ads after every third paragraph of single post content. add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { //CHANGE BELOW AdSense CODE WITH YOUR OWN CODE $ad_code = ' <script async src=""></script> <!-- inContent --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxxxxxxxxxxx" data-ad-slot="xxxxxxxxxx" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <p></p> '; if ( is_single() ) { //CHANGE 3 TO DESIRED NUMBER YOU WANT ADVERT TO BE APPEARED return prefix_insert_after_paragraph( $ad_code, 3, $content ); } return $content; } // Parent Function that makes the magic happen function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $closing_p; } if ( ( ($index + 1) % $paragraph_id ) == 0 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); }
Code language: HTML, XML (xml)

Gist link – ads-after-each-3rd-paragraph

If you find any problem or have a better way to do this please don’t forget to comment. I found that snippet in WPBeginner and I modified that to make it repeat.

Leave a Reply