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
1 Answer
Reset to default 0is_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:
Reverse the check
if(is_admin()) // if display on the dashboard, abort return; ..... your code
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 }
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
版权声明:本文标题:images - Changing default img html markup but not in admin back-end? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745411982a2657512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论