代码之家  ›  专栏  ›  技术社区  ›  user5854648 Kailash Karki

wordpress-domdocument::loadHTML():空字符串作为输入提供

  •  0
  • user5854648 Kailash Karki  · 技术社区  · 7 年前

    我在WordPress中工作,并在我的functions.php中添加了一些代码,这将阻止WordPress向图像中添加默认类并添加我自己的类。这是可行的,除非现在我创建一个新的帖子,它给我一个错误(见截图)。如果我删除代码行,它将删除错误,但我希望保留代码,因为我需要我的网站的功能。

    我查找了这个错误,即使我不理解代码中的什么导致了它,还有某人 on another Stackoverflow 建议为类似错误添加此代码:

    libxml_use_internal_errors(false);
    

    我添加了这个,但它不起作用,因为我没有完全理解它,我已经将它从这个问题中删除了,现在等待帮助。谢谢你的帮助!

    PHP

    function add_responsive_class($content){
    
            $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
            $document = new DOMDocument();
            libxml_use_internal_errors(true);
            $document->loadHTML(utf8_decode($content));
    
            $imgs = $document->getElementsByTagName('img');
            foreach ($imgs as $img) {
               $img->setAttribute('class','img-fluid');
            }
    
            $html = $document->saveHTML();
            return $html;
    }
    
    add_filter('the_content', 'add_responsive_class');
    
    // disable srcset on frontend
    add_filter('max_srcset_image_width', create_function('', 'return 1;'));
    

    Screenshot of error

    1 回复  |  直到 7 年前
        1
  •  0
  •   user5854648 Kailash Karki    7 年前

    我通过从functions.php中删除代码来解决这个问题。我接受了用户的建议,并用CSS设置了图像标签的样式,这是更好的解决方案。

    推荐文章