Last active
June 24, 2022 07:54
-
-
Save odil-io/54912ab76de667add87624d2794a62fe to your computer and use it in GitHub Desktop.
WP: Filter Content
This file contains hidden or 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 | |
function remove_lazy_load_attr( $content ) { | |
// Condition | |
if ( is_front_page() ) { | |
// Objectify DOM | |
$content = mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ); | |
$document = new DOMDocument(); | |
libxml_use_internal_errors( true ); | |
$document->loadHTML( utf8_decode( $content ) ); | |
// Get first `img` tag | |
$image = $document->getElementsByTagName( 'img' )[0]; | |
if ( is_object( $image ) ) { | |
$image->removeAttribute( 'loading' ); | |
$content = $document->saveHTML(); | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'remove_lazy_load_attr' ); |
This file contains hidden or 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 | |
function remove_lazy_load_attr_by_id( $value, $image, $context ) { | |
if ( 'the_content' === $context ) { | |
$image_url = wp_get_attachment_image_url( 4532, 'large' ); // Attachment ID and size | |
if ( false !== strpos( $image, ' src="' . $image_url . '"' ) ) { | |
return false; | |
} | |
} | |
return $value; | |
} | |
add_filter( 'wp_img_tag_add_loading_attr', 'remove_lazy_load_attr_by_id', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment