Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active July 8, 2023 13:48
Show Gist options
  • Save westonruter/a41573b932e24810b09949136b9a8445 to your computer and use it in GitHub Desktop.
Save westonruter/a41573b932e24810b09949136b9a8445 to your computer and use it in GitHub Desktop.
Plugin code to add <amp-auto-ads> on WordPress sites powered by the official AMP plugin. Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c
<?php
/**
* AMP Auto Ads for AdSense (WordPress Plugin)
*
* @package AMP_Auto_Ads
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Auto Ads for AdSense
* Description: Demonstration for how to add <code>&lt;amp-auto-ads&gt;</code> to your page. Supply AdSense publisher ID in Reading settings.
* Plugin URI: https://gist.github.com/westonruter/a41573b932e24810b09949136b9a8445
* Version: 0.4.0
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Gist Plugin URI: https://gist.github.com/westonruter/a41573b932e24810b09949136b9a8445
*/
namespace AMP_Auto_Ads;
const OPTION_NAME = 'amp_auto_ads_pub_id';
/**
* Get publisher ID.
*
* @return string ID.
*/
function get_pub_id() {
return get_option( OPTION_NAME, '' );
}
/**
* Filter plugin action links to add settings.
*
* @param string[] $action_links Action links.
* @return string[] Action links.
*/
function filter_plugin_action_links( $action_links ) {
$action_links['settings'] = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'options-reading.php' ) . '#' . OPTION_NAME ),
esc_html__( 'Settings', 'amp-auto-ads' )
);
return $action_links;
}
add_filter( 'plugin_action_links_' . str_replace( WP_PLUGIN_DIR . '/', '', __FILE__ ), __NAMESPACE__ . '\filter_plugin_action_links' );
/**
* Register setting.
*/
function register_setting() {
\register_setting(
'reading',
OPTION_NAME,
[
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
'default' => '',
'description' => esc_html__( 'AdSense Publisher ID', 'amp-auto-ads' ),
]
);
add_settings_field(
OPTION_NAME,
esc_html__( 'AdSense Publisher ID', 'amp-auto-ads' ),
function () {
printf(
'<p><input id="%s" name="%s" value="%s"></p><p class="description">%s</p>',
esc_attr( OPTION_NAME ),
esc_attr( OPTION_NAME ),
esc_attr( get_pub_id() ),
esc_html__( 'This is used for AMP Auto Ads.' )
);
},
'reading',
'default',
[
'label_for' => OPTION_NAME,
]
);
}
add_action( 'admin_init', __NAMESPACE__ . '\register_setting' );
/**
* Print the amp-auto-ads element.
*/
function print_component() {
$pub_id = get_pub_id();
if ( empty( $pub_id ) ) {
return;
}
printf( '<amp-auto-ads type="adsense" data-ad-client="%s"></amp-auto-ads>', esc_attr( $pub_id ) );
}
// Print the <amp-auto-ads> component at the start of the <body> for themes that support wp_body_open().
add_action(
'wp_body_open',
function() {
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
print_component();
}
}
);
/*
* If theme does does not support wp_body_open, then this fallback is used to print the component int he footer.
* The docs specify that <amp-auto-ads> *should* be placed at the start of the <body> but this is not a hard requirement.
*/
add_action(
'wp_footer',
function() {
if ( ! did_action( 'wp_body_open' ) && function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
print_component();
}
}
);
// Classic mode.
add_filter(
'amp_post_template_data',
function( $data ) {
$data['amp_component_scripts'] = array_merge(
$data['amp_component_scripts'],
[
'amp-auto-ads' => true,
]
);
return $data;
}
);
add_action( 'amp_post_template_body_open', __NAMESPACE__ . '\print_component' ); // Available as of AMP v1.2.1.
add_action(
'amp_post_template_footer',
function() {
if ( ! did_action( 'amp_post_template_body_open' ) ) {
print_component();
}
}
);
@westonruter
Copy link
Author

@ashishguptastar
Copy link

ashishguptastar commented Jun 4, 2020

i am facing issue on my website after install this plugin log.js:258 [amp-auto-ads] No ad placements found for publisher ID 'ca-pub-92201985XXXXXXXX'. Check that the provided publisher ID is correct.
nielitexams.com

i have masked my publisher id

@hieptd
Copy link

hieptd commented Aug 16, 2020

I got an error: <script custom-element="amp-auto-ads" src="http://1" async=""></script> when using the plugin.
Also when checking at F12 Google Chrome, the Console item I get an error.
Mixed Content: The page at 'https://www.ahisu.com/' was loaded over HTTPS, but requested an insecure script 'http://0.0.0.1/'. This request has been blocked; the content must be served over HTTPS.

@hello2majid
Copy link

hi,
First of thank you very much for providing this free plugin for AMP support.
I have installed this plugin and inserted the Google AdSense publisher ID but still getting the same error "Looks like you've got an AMP site. Make sure you're using Auto ads for AMP".

I need help

@westonruter
Copy link
Author

For support, please refer to the AMP plugin support forum: https://wordpress.org/support/plugin/amp/

Please search for amp-auto-ads to find existing topics, as there have been questions before.

@hello2majid
Copy link

Thanks for the reply
Actually, I was trying to implement AMP ads on my site www.newst20.com when I found this forum on Google.

I have installed the plugin, but Adsense is not detecting the Auto ads code.
Now switching to /support/plugin/amp/
Thanks for your support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment