Skip to content

Instantly share code, notes, and snippets.

@odil-io
Last active June 24, 2022 07:54
Show Gist options
  • Save odil-io/54912ab76de667add87624d2794a62fe to your computer and use it in GitHub Desktop.
Save odil-io/54912ab76de667add87624d2794a62fe to your computer and use it in GitHub Desktop.
WP: Filter Content
<?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' );
<?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