Buy me a Coffee

How to setup Adsense on AMP WordPress

1 min read

WordPress powered sites are getting a recommendation by AdSense to support AMP(Accelerated Mobile Pages), you can see this recommendation in the Google AdSense Dashboard.

AMP regulation for designing mobile optimized version of any given page, where the stylesheet is inserted in the page itself inside the head tag and loading of scripts and media is done async. You can read the documentation to understand. It improves the performance of mobile devices that are running on 3G or slow internet connection.

Google offers AMP search result to mobile devices on a slow network, you could still access these pages by adding /amp to your post URL (example).

What made me happy and accept this change was Automattic official AMP plugin.

Steps to install AdSense in AMP plugin
Go to Plugins > Installed Plugins > AMP

Click edit (near “deactivate”)

Choose single.php(amp/templates/single.php) in the right files sidebar.

Insert the following snippet wherever you prefer having the ad show up, with the required changes.

If you find the above complex you can replace the contents of single.php by replacing the contents with the snippet below

get( 'html_tag_attributes' ) ); ?>> load_parts( array( 'header-bar' ) ); ?>

get( 'post_title' ) ); ?>

load_parts( apply_filters( 'amp_post_article_header_meta', array( 'meta-author', 'meta-time' ) ) ); ?>
load_parts( array( 'featured-image' ) ); ?>
get( 'post_amp_content' ); /* amphtml content; no kses */ ?>
load_parts( apply_filters( 'amp_post_article_footer_meta', array( 'meta-taxonomy', 'meta-comments-link' ) ) ); ?>
load_parts( array( 'footer' ) ); ?>

I have made a simplified version of this, all you need to do it add your publisher id and you Ad slot to the variables.

Hit Update File and the changes should take place immediately.

If you have any issues with this leave a comment below, feedback is always accepted.