WordPress AMP Plugin – Use Adsense in Classic Mode

There are 2 popular AMP plugins for WordPress:

  1. Official WordPress AMP plugin: AMP
  2. 3rd party AMP plugin: AMP for WP

AMP for WP should have an option for Adsense. But official one not.

I have made small inspection and noticed that all existing adsense codes are converted to amp tags – without any issues. But there is not any Ads JS file which should render these tags.

Its documentation is not good and doesn’t provide easy to fix information about this.

First i tried to add the needed JS code (for all type of ads) and tag (for auto-ads) correspondingly with wp_head and wp_footer native hookd. But no luck.

Classic mode of AMP plugin doesn’t fire any UI hook. Because it has own small template file.

So i decided to inspect the plugin’s code and to see which hooks it has for own small template.

Here is how it is seen:

As you see, this template doesn’t fire wp_head and wp_footer hooks. (Click to see a larger image)

So, that is it – it has own amp_post_template_head and amp_post_template_footer hooks. By using that i have solved the problem.

Here is the code:

And of course, don’t forget to enable AMP Ads in your Adsense Dashboard (via Left menu -> Auto Ads -> Auto Ads for AMP)

Get more useful WP tricks and snippets by subscribing to my mailclub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.