Last active
July 31, 2018 00:12
-
-
Save westonruter/b754411e792d137b7d92045adc2f833d to your computer and use it in GitHub Desktop.
Add noloading to all images in an AMP page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', '' ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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