Changing default img html markup but not in admin back-end?

Based on some posts on Stack and ACF forum I’ve created code for changing default img html markup for lazyload script lozad.js. It’s works great, but works also in admin back-end. I was trying to use if( ! is_admin(); ) but with no effects. How can I fix that?

<?php // Modify img markup for lazy load
add_filter( ‘wp_get_attachment_image_attributes’, ‘gs_change_attachment_image_markup’ );

function gs_change_attachment_image_markup($attributes){
if (isset($attributes[‘src’])) {
$attributes[‘data-src’] = $attributes[‘src’];
$attributes[‘src’] = ”; //could add default small image or a base64 encoded small image here
}
if (isset($attributes[‘srcset’])) {
$attributes[‘data-srcset’] = $attributes[‘srcset’];
$attributes[‘srcset’] = ”;
}
$attributes[‘class’] .= ‘ lozad’;
return $attributes;
}
add_filter(‘the_content’, ‘content_image_markup’, 15);

function content_image_markup($the_content) {
$thecontent = get_the_content();
if(!empty($thecontent)) {
libxml_use_internal_errors(true);
$post = new DOMDocument();
//$post->loadHTML($the_content);
$post->loadHTML(mb_convert_encoding($the_content, ‘HTML-ENTITIES’, ‘UTF-8’));
$imgs = $post->getElementsByTagName(‘img’);
// Iterate each img tag
foreach( $imgs as $img ) {
if( $img->hasAttribute(‘data-src’) ) continue;
if( $img->parentNode->tagName == ‘noscript’ ) continue;
$clone = $img->cloneNode();
$src = $img->getAttribute(‘src’);
$img->removeAttribute(‘src’);
$img->setAttribute(‘data-src’, $src);
$srcset = $img->getAttribute(‘srcset’);
$img->removeAttribute(‘srcset’);
if( ! empty($srcset)) {
$img->setAttribute(‘data-srcset’, $srcset);
}
$imgClass = $img->getAttribute(‘class’);
$img->setAttribute(‘class’, $imgClass . ‘ lozad’);
$no_script = $post->createElement(‘noscript’);
$no_script->appendChild($clone);
$img->parentNode->insertBefore($no_script, $img);
};
return $post->saveHTML();
}
}
add_filter(‘acf_the_content’, ‘acf_content_image_markup’, 15);

function acf_content_image_markup($the_content) {
libxml_use_internal_errors(true);
$post = new DOMDocument();
//$post->loadHTML($the_content);
$post->loadHTML(mb_convert_encoding($the_content, ‘HTML-ENTITIES’, ‘UTF-8’));
$imgs = $post->getElementsByTagName(‘img’);
// Iterate each img tag
foreach( $imgs as $img ) {
if( $img->hasAttribute(‘data-src’) ) continue;
if( $img->parentNode->tagName == ‘noscript’ ) continue;
$clone = $img->cloneNode();
$src = $img->getAttribute(‘src’);
$img->removeAttribute(‘src’);
$img->setAttribute(‘data-src’, $src);
$srcset = $img->getAttribute(‘srcset’);
$img->removeAttribute(‘srcset’);
if( ! empty($srcset)) {
$img->setAttribute(‘data-srcset’, $srcset);
}
$imgClass = $img->getAttribute(‘class’);
$img->setAttribute(‘class’, $imgClass . ‘ lozad’);
$no_script = $post->createElement(‘noscript’);
$no_script->appendChild($clone);
$img->parentNode->insertBefore($no_script, $img);
};
return $post->saveHTML();
}
function image_tag_class($class, $id, $align, $size) {
return $class . ‘ lozad’;
}
add_filter(‘get_image_tag_class’, ‘image_tag_class’, 10, 4);

?>

Read more here:: Changing default img html markup but not in admin back-end?

Leave a Reply

Your email address will not be published. Required fields are marked *