Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active December 21, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/e019e53d4e34235907138eae31897e46 to your computer and use it in GitHub Desktop.
Save westonruter/e019e53d4e34235907138eae31897e46 to your computer and use it in GitHub Desktop.
<?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
Copy link
Author

@westonruter
Copy link
Author

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