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 – https://sangams.com.np/how-to-make-adsense-work-with-amp-desktop-version/

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="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></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 );
}

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.

This Post Has 10 Comments

  1. Manuel

    Thanks, it works for me, but I need a feature, can the magic only happen if there are more than 1500 characters in the Post? How can I do this?
    Thanks a lot!

  2. George

    Can you provide the code, so this works every 12 paragraphs on WP Pages main content?

    1. Sangam

      I have commented in the code like this. //CHANGE 3 TO DESIRED NUMBER YOU WANT ADVERT TO BE APPEARED. Find the number 3 and update it to 12.

  3. naveen

    is this works with blogspot blogs

    1. Sangam

      Sorry it will not work. It is for WordPress.

  4. Shoumya Chowdhury

    If I use the php code, how can I exclude 2-3 particular posts??.

  5. Adeniyi Badmus

    Thanks. I initially copied <?php with the snippet. Now is working. But I have another concern pls. There is not much space between the paragraphs and the ads. How do I add some space, please?

    1. Sangam

      Do you see the [code]

      [/code] tag at last in the $ad_code variable? That is used to add extra space in after paragraph. You can use it at beginning of $ad_code too.

  6. Adeniyi Badmus

    hello, please help, this is the error i get

    Your PHP code changes were rolled back due to an error on line 70 of file files/themes/expertly-child/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘<', expecting end of file

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.