Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active July 31, 2018 00:12
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/b754411e792d137b7d92045adc2f833d to your computer and use it in GitHub Desktop.
Save westonruter/b754411e792d137b7d92045adc2f833d to your computer and use it in GitHub Desktop.
Add noloading to all images in an AMP page
<?php
/**
* Make sure that all <amp-img> elements have the noloading attribute.
*/
class Img_Noloading_Sanitizer extends AMP_Base_Sanitizer {
public function sanitize() {
foreach ( $this->dom->getElementsByTagName( 'amp-img' ) as $img ) {
$img->setAttribute( 'noloading', '' );
}
}
}
<?php
add_filter( 'amp_content_sanitizers', function( $sanitizers ) {
require_once __DIR__ . '/class-img-noloading-sanitizer.php';
return array_merge(
$sanitizers,
array(
'Img_Noloading_Sanitizer' => array(),
)
);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment