admin管理员组

文章数量:1425676

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
      }
    

本文标签: imagesChanging default img html markup but not in admin backend