根据Stack和ACF论坛上的一些帖子,我创建了更改lazyload脚本lozad的默认img html标记的代码。js。它工作得很好,但在管理后端也可以工作。我想用if( ! is_admin(); )
但没有效果。我怎样才能解决这个问题?
<?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);
?>
最合适的回答,由SO网友:Debbie Kurth 整理而成
is\\u admin()是一个调用,它不验证当前用户角色是否正确,而是检查代码是否试图显示在后端/管理/仪表板区域。
https://codex.wordpress.org/Function_Reference/is_admin
您可以尝试以下三种解决方案之一:
撤销支票
if(is_admin()) // if display on the dashboard, abort
return;
..... your code
检查当前用户检查的用户功能,以及是否显示仪表板/管理员:
if(!current_user_can(\'manage_options\') && is_admin())
{
Your code
}
检查实际角色,默认情况下,不显示管理员:
function is_this_admin_user()
{
return in_array(\'administrator\', wp_get_current_user()->roles);
}
if(is_this_admin_user() == 0)
{
....your code
}