• Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / wordpress tutorials for beginners advanced complete guide to run WordPress / How to add adsense amp ads to wordpress

How to add adsense amp ads to wordpress

Default plugin of AMP for wp allows the only Header and above the content. But Not supports within paragraph and Sticky ad also sticky banner ad above.

How to add Adsense ad in amp version WordPress

Above the fold ad

<amp-ad
layout=”fixed-height”
height=100
type=”adsense”
data-ad-client=”ca-pub-xxxx”
data-ad-slot=”xxxxx”>
</amp-ad>

Table of Contents

Toggle
    • Insert Post Ads plugins
    • Post Adverts plugin
  • Adding Adsense ads with AMP for wp plugin within content
  • AMP Sticky Ad
  • AMP Auto Ads
    • Below Script In Body:
    • How to insert Head & Body Tag in AMP Pages WordPress?
    • Remove / Update ? clear AMP Google cache

Insert Post Ads plugins

(lh-multisite-ads.php)

 

Below the fold

<amp-ad
layout=”responsive”
width=300
height=250
type=”adsense”
data-ad-client=”ca-pub-xxx”
data-ad-slot=”xxx”>
</amp-ad>

<amp-ad width=350 height=100

type=”adsense”

data-ad-client=”ca-pub-xxxx”
data-ad-slot=”xxx”>

</amp-ad>

<amp-ad
layout=”responsive”
width=300
height=250
type=”adsense”
data-ad-client=”ca-pub-xxxx”
data-ad-slot=”xxxx”>
</amp-ad>

  1. Open Plugin Editor (Not Theme Editor).
  2. Choose AMP plugin and Press Select Button.
  3. Open amp/templates/single.php file.
  4. Find <div class=”amp-wp-content”> line. And below the line paste our AMP Comfortable Ad code (Which described above paragraph).
  5. Find do_action( ‘amp_post_template_footer’, $this ); before the php starting tag paste Google AMP Ad code.
  6. Save the file. All Set.

Post Adverts plugin

 

Make home page to AMP version

works by plugin Accelerator mobile pages for WordPress.

 

Adding Adsense ads with AMP for wp plugin within content

After any paragraph 2& 5 or

 

/wp-content/plugins/accelerated-mobile-pages/templates/design-manager/design-3/elements/content.php

based on which design you are using on accelerated-mobile-pages by default its 2

// Normal Front Page Content
if ( ! $amp_custom_content_enable ) {
$ampforwp_the_content = $this->get( ‘post_amp_content’ ); // amphtml content; no kses

(replace by below)

$content = $this->get( ‘post_amp_content’ );
$contents = explode(“</p>”, $content);
$p_number = 1;
foreach($contents as $content){
echo $content;
/* Ad after 2nd paragraph */
if($p_number == ‘2’){
echo’ <center><amp-ad class=”amp-ad-1″ type=”adsense” width=300 height=100 data-ad-client=”ca-pub-xxx” data-ad-slot=”xxxx”></amp-ad></center>’; } /* Ad after 5th paragraph */ if($p_number == ‘5’){ echo ‘<p></p>’; echo'<center> <amp-ad class=”amp-ad-5″ type=”adsense” width=300 height=250 data-ad-client=”ca-pub-xxxx” data-ad-slot=”xxx”></amp-ad></center>’; }
echo ‘</p>’; $p_number++;
}

 

 

 

AMP Sticky Ad

Header Script:

<script async custom-element=”amp-sticky-ad” src=”https://cdn.ampproject.org/v0/amp-sticky-ad-1.0.js”></script>

Body tag

<amp-sticky-ad layout=”nodisplay”>
<amp-ad width=”320″
height=”50″
type=”adsense”

data-ad-client=”ca-pub-xxxx”
data-slot=”slot id”>
</amp-ad>
</amp-sticky-ad>

AMP Auto Ads

Only Upto 3 ads available with amp auto ads

<script async custom-element=”amp-auto-ads” src=”https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js”></script>

wp-content/plugins/accelerated-mobile-pages/includes/vendor/amp/templates/ header-bar.php

place above script in the header

 

Below Script In Body:

<amp-auto-ads type=”adsense” data-ad-client=”ca-pub-XXXXXX”></amp-auto-ads>

wp-content/plugins/accelerated-mobile-pages/includes/vendor/amp/templates/footer.php

How to insert Head & Body Tag in AMP Pages WordPress?

We can easily insert header and body section of the default amp plug in by Automattic. but AMP for WP Once we insert after the updating the code will vanish.

so we need to use a plugin, unfortunately, there are amp plugins not yet launched on WordPress plugin directory.

How to Find Header Php in AMp for wp Plugin

Adding code to header section

/*
* Adding Header and Footer Code in AMP pages in WordPress
*/
add_action(‘amp_post_template_header’,’awts_amp_my_custom_head’);
function awts_amp_my_custom_head($amp_template) {
?>
<script async custom-element=”amp-auto-ads” src=”https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js”></script><?php
}

//BOdy Tag

/*
* Adding Header and Footer Code in AMP pages in WordPress
*/
add_action(‘amp_post_template_footer’,’awts_amp_my_custom_footer’);
function awts_amp_my_custom_footer($amp_template) {
?>
<amp-auto-ads type=”adsense” data-ad-client=”ca-pub-XXXXXX”></amp-auto-ads><?php
}

 

AMP Plugin by WordPress Add header to Auto Ads & Sticky Ads

plugin >> editor>>AMP>>Templates>>header-bar.php

js files (auto ads, sticky ads.js amp ads js)

Before closing body tag

Templates>> html-end.php

<amp-sticky-ad layout=”nodisplay”>
<amp-ad width=”320″
height=”50″
type=”adsense”

data-ad-client=”ca-pub-xxxxxx”
data-slot=”slot id”>
</amp-ad>
</amp-sticky-ad>

Remove / Update ? clear AMP Google cache

Generally, Google Removes amp cache automatically within 3 days after a lot of 404 errors.But we can remove the content quickly within hours or within a day by sending an update ping request or terminal. But here is a simple way clear amp cache.

just append your URL to clear the cache

https://cdn.ampproject.org/c/s/YourURLpathwithouthttp

the above URL redirects to a NON AMP version.

Removing entire website cache may not works.

 

Ask a Question:

You must be logged in to post a comment.

wordpress tutorials for beginners advanced complete guide to run WordPress

  • wordpress speed optimization Speed up wordpress Score on Google pagespeed
  • best wordpress plugins social, seo, Backup, speed up your blog
  • TOP 10 Wordpress Themes Premium/ Free & providers
  • Wordpress Adsense Plugins & Manually
  • Social share buttons for website Wordpress Blogger
  • How to bulk delete in wordpress posts, pages, Media Images, tags and categories
  • TOP 5 Best wordpress comment plugins 2020 (disable pingback, replytocom, customize form)
  • all in one seo pack vs yoast seo pressor
  • Auto post to Facebook, twitter, Google Plus Wordpress Posts 2019
  • wordpress plugins that slow down your site and its alternatives
  • How to use wordpress tags and categories
  • Wordpress duplicate thin content Issues {FIXED}
  • how to increase wordpress memory & file upload size
  • combine rss feeds wordpress
  • Reduce the impact of third-party code ,optimize JS delivery, Minify, remove unused js, pre connect, inline, lazy load etc
  • change wordpress site url internal links in mysql command phpmyadmin
  • install wordpress on ubuntu nginx lemp command line apache debian
  • Wordpress Security Plugins 2021 (Brute force, Login lockdown, Malware scanner etc)
  • website cache best caching plugins wordpress w3tc cloudflare
  • How to add adsense amp ads to wordpress
  • wordpress password reset in 3 ways mail, phpmyadmin, mysql command
  • how i recovered hacked website site 5 times ft wordpress?
  • wordpress redirection examples Rankmath seo
  • enable browser caching in wordpress apache nginx caching headers explained
  • W3TC w3 total cache plugin setup guide review comparison with wp rocket and wp super cache
  • Wordpress database optimization (don't neglect) plugins & phpmyadmin manually
  • Optimize css delivery inline critical css, remove unused css, render blocking css
  • Converting WordPress into static website speed after 1 second dynamic 3 seconds
  • Wordpress errors and fix
  • Rules to get 100/100 in Google pagespeed web core vitalsscore
  • Install xamp & wordpress on localhost windows
  • favicon ico Change / Add / Disable favicon in wordpress Ft Genesis Theme or any theme
  • increase wordpress memory limit (how to ) 40MB , 64 MB 128 MB or 512 MB maybe 1024MB
  • wp cron job check working or not disable enable change schedule
  • google analytics plugins for wordpress #sitekit #monsterinsights gadwp back as gainwp
  • Lazy Load Adsense to improve core web vitals - Vs revenue
  • xmlrpc php wordpress uses, disable without 403 error log message
  • wordpress 304 header implement using plugin
  • wordpress change post date programmatically (auto on update like blogger
  • sorry you are not allowed to access this page. admin WordPress options table
  • clone wordpress in linux ubuntu server lemp nginx
  • Cloudflare cache rules for WordPress along with page rules cache level everything
  • wordfence-waf.php fatal error fixed after migration
  • Cloudflare page rules cache level everything redirect and sub domain
  • wordpress delete all posts from database mysql query for phpmyadmin
  • php fpm oom killer problem how to dig it.. memory pool and execution time
  • nginx wordpress multisite subdirectory multiple domains
  • Helpful Content update recovery Trying to dig it
  • How to Move a Single WordPress Site into a Multisite
  • Enable multisite in wordpress in 3 steps problems datbase error
  • Seprate single site from wordpress multisite domain based
  • Error establishing a database connection wordpress multisite
  • Add single wordpress to MultiSite wordpress
  • genesis theme cateogory customization examples
  • could not insert term into the database. wordpress
  • wordpress breadcrumbs not showing full path
  • There has been a critical error on this website fed up
  • Site structure WP categories vs sub pages or multisite posts or pages
  • WordPress pyramid structure Permalinks Category vs sub pages vs multisite

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy