insert-adverts

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.

10 thoughts on “How to Insert Auto Repeating AdSense Ads Between Paragraphs Without Plugin”

  1. 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. 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. 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.

  3. 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 Comment

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