How to insert ads within the post content in AMP pages of WordPress

insert ads within the post content in AMP

AMP content is important if you want better visibility on Google search. If you own a news website, you can get a chance to feature in top carousel results shown as top stories. If you are still not sure what is AMP, read out article on AMP. If you want to install AMP on your WordPress website, read how to setup AMP on WordPress.

After setting up the AMP, the most important thing we think about adding ads. The native AMP plugin by Automattic does not offer option to insert ads. There are few plugins but they only offer ads at top and bottom of the content. I also wrote how to add Adsense ads on AMP pages. But, if you want to insert ads within the content, you need to buy the premium version of the plugin.

If you are not willing to pay and are ready to tweak code of your WordPress theme, you can manually add code in your blog’s AMP pages. You just need to copy and paste this code in your theme’s functions.php file. Make sure you have already AMP setup on your blog before using this code.

Insert ads within the post content in AMP

Add the given code in your theme’s functions.php file and make sure to alter the Adsense ads code.

add_filter( 'the_content', 'qwr_insert_post_ads' );
 
function qwr_insert_post_ads( $content ) {
     
$ad_code = '<amp-ad
layout="responsive"
width=300
height=250
type="adsense"
data-ad-client="ca-pub-xxxxxxxxxxx"
data-ad-slot="xxxxxxx">
</amp-ad>';
 
    if ( is_single() && ! is_admin() ) {
        if(is_amp_endpoint())
        {
            $content = $adcode. $content; // insert ad at beginning of content
            $content = qwr_insert_after_paragraph1( $ad_code, 2, $content ); // insert ad after 2nd paragraph
            $content = qwr_insert_after_paragraph1( $ad_code, 5, $content ); // insert ad after 5th paragraph
            $content = $content.$adcode; // insert ad at last of content
            return $content;
        }
    }
     
    return $content;
}
  
// Parent Function that insert ads in paragraphs
  
function qwr_insert_after_paragraph1( $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 ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
     
    return implode( '', $paragraphs );
}

You only need to add your AMP ad code in place of the sample code.

I use the same code on my website Techlomedia and it really helped me. Because people generally ignore ads at top and bottom of the content. In-content ads work better. I have also seen improvements on my Adsense earnings after setting up the in-content ads on AMP. So, you should also do the same.

If you find it hard to implement AMP ads by using the code, you can always ask via comment. I will surely help you.

Tags: | |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker