Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
}
}
);
@naftalina

This comment has been minimized.

Copy link

@naftalina naftalina commented Mar 14, 2019

Hi can I know where I have to place the file?

@amedina

This comment has been minimized.

Copy link

@amedina amedina commented May 21, 2019

You could: put the add the above plugin in the plugin directory of your site (with a name of your choice), and then activate the plugin on your admin screen; or create a child theme and put the functionality above into the functions.php file of that child theme.

@igecorner

This comment has been minimized.

Copy link

@igecorner igecorner commented Jun 14, 2019

Hi ,

I have created this plugin but still no luck

I am getting " No ad placements found for publisher ID 'ca-pub-xxxxxxxxxx'. Check that the provided publisher ID is correct." this issue ?

please let me know how to resolve this ?

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Jun 14, 2019

@igecorner ca-pub-xxxxxxxxxx does no look like a valid publisher ID. Please ensure you get the ID from your AdSense account.

@igecorner

This comment has been minimized.

Copy link

@igecorner igecorner commented Jun 14, 2019

Thanks for the reply i have valid adsense account. Ads are showing in non-amp page, but no luck on amp page, please check the website here

AMP PAGE : https://tinyurl.com/y3zonknq

Non Amp page (Home Page) : https://tinyurl.com/yyzcu7pc

Please guide me.

Thanks

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Jun 14, 2019

@igecorner I'm not an expert on AdSense. So I suggest connecting AdSense Support directly: https://support.google.com/adsense/

Or else inquire support from the AMP project generally: https://github.com/ampproject/amphtml/blob/master/README.md#having-a-problem-using-amp

@igecorner

This comment has been minimized.

Copy link

@igecorner igecorner commented Jun 14, 2019

I have already raised, but no solution yet .....
ampproject/amphtml#21955

@igecorner

This comment has been minimized.

Copy link

@igecorner igecorner commented Jun 14, 2019

@westonruter . finally this issue is fixed, google is providing new amp ad code now, that works for me
Thanks for your time and reply.

@amedina

This comment has been minimized.

Copy link

@amedina amedina commented Jun 14, 2019

Notice that the tag should be placed as the first child of the , as described in the documentation. In the link provided I believe the component is not located there.

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Jun 14, 2019

@igecorner What is the new ad code you were provided?

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Jun 14, 2019

@amedina I've updated the code to leverage wp_body_open if the theme supports it.

@igecorner

This comment has been minimized.

Copy link

@igecorner igecorner commented Jun 15, 2019

@westonruter , still auto amp ad code not working, but google starts providing AMP ad code, that is working.

I mean non-auto amp code is working now, but need to find the solution for auto amp code, i have placed both auto-amp and amp code in the link below, you can see the error in chrome console

"log.js:184 No ad placements found for publisher ID......................"

But for me at least i can show some ad in the page

AMP PAGE : https://tinyurl.com/y3zonknq

@francacps

This comment has been minimized.

Copy link

@francacps francacps commented Nov 20, 2019

Very good this last update. Thanks!

@Ganka22

This comment has been minimized.

Copy link

@Ganka22 Ganka22 commented Dec 6, 2019

Hi,

We have some problems with this plugin. Everything seems to be installed correctly but several days later we still cannot see Auto ads on our AMP website.

We use the Twenty Twenty theme and the official AMP plugin for WordPress for our website https://www.helpful-kitchen-tips.com

Meanwhile, we tried the Site Kit plugin as well in order to place Auto ads on the above mentioned AMP website but again without success, which makes me think that the problem is not in the plugins at all.

@shkataya

This comment has been minimized.

Copy link

@shkataya shkataya commented Dec 9, 2019

@Ganka22 In case that Auto Ads is not working on AMP pages, I'd recommend that you place some manual ads on the AMP pages. Auto Ads doesn't guarantee that ads will be placed every time.

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Apr 4, 2020

@ashishguptastar

This comment has been minimized.

Copy link

@ashishguptastar 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

This comment has been minimized.

Copy link

@hieptd 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.