Skip to content

Instantly share code, notes, and snippets.

@SanjeevMohindra
Last active June 25, 2023 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SanjeevMohindra/4a9c83c17daea79f5038e41edf029926 to your computer and use it in GitHub Desktop.
Save SanjeevMohindra/4a9c83c17daea79f5038e41edf029926 to your computer and use it in GitHub Desktop.
Add Google Adsense Ads to AMP Pages
<?php
// Do not copy the above php tag in your function.php
/**
* Add Google Adsense Ad on top of the post content
*
* Add this to your function.php
*
* @author MetaBlogue
* @license GPL-2.0+
* @link https://metablogue.com/configure-enable-amp-wordpress/
*/
/**
* Add Google Adsense code to AMP above the content
*/
add_action( 'pre_amp_render_post', 'isa_amp_add_content_filter' );
function isa_amp_add_content_filter() {
add_filter( 'the_content', 'isa_amp_adsense_above_content' );
}
function isa_amp_adsense_above_content( $content ) {
$publisher_id = 'ca-pub-5750478706187616';
$ad_slot = '3512136773';
// Add Adsense ad above AMP content
$ad_code = '<amp-ad layout="fixed-height" height="100" type="adsense" data-ad-client="' . $publisher_id . '" data-ad-slot="' . $ad_slot . '"></amp-ad>';
return $ad_code . $content . $ad_code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment