<?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><amp-auto-ads></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(); | |
} | |
} | |
); |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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 ? |
This comment has been minimized.
This comment has been minimized.
@igecorner |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
@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 |
This comment has been minimized.
This comment has been minimized.
I have already raised, but no solution yet ..... |
This comment has been minimized.
This comment has been minimized.
@westonruter . finally this issue is fixed, google is providing new amp ad code now, that works for me |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
@igecorner What is the new ad code you were provided? |
This comment has been minimized.
This comment has been minimized.
@amedina I've updated the code to leverage |
This comment has been minimized.
This comment has been minimized.
@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
But for me at least i can show some ad in the page AMP PAGE : https://tinyurl.com/y3zonknq |
This comment has been minimized.
This comment has been minimized.
Very good this last update. Thanks! |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
@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. |
This comment has been minimized.
This comment has been minimized.
Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c |
This comment has been minimized.
This comment has been minimized.
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. i have masked my publisher id |
This comment has been minimized.
This comment has been minimized.
I got an error: <script custom-element="amp-auto-ads" src="http://1" async=""></script> when using the plugin. |
This comment has been minimized.
This comment has been minimized.
hi, I need help |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
Thanks for the reply I have installed the plugin, but Adsense is not detecting the Auto ads code. |
This comment has been minimized.
Hi can I know where I have to place the file?