Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* AMP Suppress Eager Loading Images plugin bootstrap.
*
* @package Google\AMP_Suppress_Eager_Loading_Images
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2020 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Suppress Eager Loading Images
* Plugin URI: https://gist.github.com/westonruter/e019e53d4e34235907138eae31897e46
* Description: Prevent images (such as featured images in the Twenty Twenty-One theme) from causing AMP validation errors for the loading=eager attribute.
* Version: 0.1
* 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/e019e53d4e34235907138eae31897e46
*/
add_action(
'wp',
function () {
if ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() ) {
return;
}
add_filter(
'wp_get_attachment_image_attributes',
function ( $attr ) {
if ( isset( $attr['loading'] ) && 'eager' === $attr['loading'] ) {
unset( $attr['loading'] );
}
return $attr;
}
);
}
);
@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Dec 21, 2020

@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Dec 21, 2020

This should be made obsolete by ampproject/amp-wp#5721.

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