Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Disable theme lazy images by swapping [data-*] attributes with the original image attributes
- function filter_disable_lazy_images( $buffer ) {
- libxml_use_internal_errors( true );
- $document = new DOMDocument();
- $document->loadHTML( mb_convert_encoding( $buffer, 'HTML-ENTITIES', 'UTF-8' ) );
- $lazy_images = $document->getElementsByTagName( 'img' );
- foreach ( $lazy_images as $image ) {
- $src = $image->getAttribute( 'data-src' );
- $srcset = $image->getAttribute( 'data-srcset' );
- $sizes = $image->getAttribute( 'data-sizes' );
- if ( ! empty( $src ) ) {
- $image->setAttribute( 'src', $src );
- $image->removeAttribute( 'data-src' );
- $image->setAttribute( 'loading', 'lazy' );
- }
- if ( ! empty( $srcset ) ) {
- $image->setAttribute( 'srcset', $srcset );
- $image->removeAttribute( 'data-srcset' );
- }
- if ( ! empty( $sizes ) ) {
- $image->setAttribute( 'sizes', $sizes );
- $image->removeAttribute( 'data-sizes' );
- }
- };
- return $document->saveHTML();
- }
- function buffer_start() {
- ob_start( 'filter_disable_lazy_images' );
- }
- function buffer_end() {
- ob_end_flush();
- }
- add_action( 'wp_head', 'buffer_start' );
- add_action( 'wp_footer', 'buffer_end' );
Add Comment
Please, Sign In to add comment