I am working in WordPress and added some code in my functions.php that will prevent WordPress from adding default classes to the image and add my own class. This works except now when I create a new post it gives me an error (see screenshot). If I remove the lines of code it removes the error but I'd like to keep the code as I need the function for my website.
I looked this error up even though I don't understand what in my code is causing it and someone on another Stackoverflow suggested adding this code for a similar error:
libxml_use_internal_errors(false);
I added this and it did not work, since I did not fully understand it I have removed it for now pending help from this question. I appreciate any help!
functions.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;'));