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

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