images - 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 scri

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);

?>

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);

?>
Share Improve this question asked Jun 12, 2019 at 0:15 Piotr MileckiPiotr Milecki 253 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

is_admin() is a call that does not verify if the current user role, but rather it was made to check if the code is trying to display in the backend/admin/dashboard area.

https://codex.wordpress/Function_Reference/is_admin

You can try one of three solutions:

  1. Reverse the check

        if(is_admin())  // if display on the dashboard, abort
            return;
    
        ..... your code 
    
  2. Check for the user capabilities with the current user check and if the dashboard/admin is being displayed:

      if(!current_user_can('manage_options') && is_admin())
        {
         Your code
        }
    
  3. Check the actual roles and by default, therefore not display the admin:

    function is_this_admin_user()
      {
       return in_array('administrator',  wp_get_current_user()->roles);
      }
    if(is_this_admin_user() == 0)
      {
      ....your code
      }
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745411982a4626576.html

相关推荐

  • images - 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 scri

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信