Looking to generate some revenue by adding AMP Ads to your WordPress theme? Follow this guide to add Google AdSense to your website.
Add Google AdSense to your AMP website
To add Google AdSense to your AMP website, follow the steps below to add them to your post singles.
Add code to header.php
- Open your theme’s header.php file.
- Add the code below to register the AMP Ads script, hosted by amp.dev in between your <head></head> tags.
- Wrap an if statement to pass is_singular( ‘post’ ) around the script to only use the script where Ads are to be shown appropriately.
<?php if ( is_singular( 'post' ) ) : ?> <script async custom-element='amp-ad' src='https://cdn.ampproject.org/v0/amp-ad-0.1.js'></script> <?php endif; ?>
Note: please, be sure you’ve got the latest version of this required script.
Add code to single.php
- Open your theme’s single.php file.
- Copy and paste the code below, using your Ad client-ID and slot (provided by Google AdSense).
<amp-ad width="300" height="300" type="adsense" data-ad-client="ca-pub-XXXXXXXXXXXXXXXX" data-ad-slot="XXXXXXXXXX"> <div overflow></div> </amp-ad>