0

So I'm using this code to add an extra class to all the images that load in the post and page content, and everything works good, but if there's no image I get the error also sometimes the error loads before loading the image.

This is the code

function add_img_post_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) {
       $existing_class = $img->getAttribute('class');
       $img->setAttribute('class',"$existing_class post-image lazy-load o-image");
    }
    $html = $document->saveHTML();
    return $html;
}
add_filter ('the_content', 'add_img_post_class');

And the error seems to be on the 5th line:

$document->loadHTML(utf8_decode($content));
  • Also, I'm getting the error sometimes after saving content on the CMS, it will just take me out of the admin and show the error, but still saves all the content – alejuu Jul 11 '17 at 20:09
  • On the pages that cause the error, can you find out what utf8_decode($content) is returning? – czerspalace Jul 11 '17 at 20:28
  • I think I don't understand your question, so I just realize that in the pages that I get the error is because there's not a featured image or any image inside the post, when I get the error I just get my normal single page and where the featured image should appear it appears the warning because there's not an image. – alejuu Jul 11 '17 at 20:41
  • I meant separate the utf8_decode($content) line out of the loadHTML, so do something like $utfContent = utf8_decode($content); echo "Content =({$utfContent})"; $document->loadHTML($utfContent); – czerspalace Jul 11 '17 at 21:57
0

Here's an updated version of the code that will avoid the warning by adding a simple check that will return $content unmodified if it's an empty string.

The original code adds <html> to the modified markup (check the page source), which we don't want. The code below fixes that too.

add_filter( 'the_content', 'wpse_add_img_post_class' );
function wpse_add_img_post_class( $content ) {
    // Bail if there is no content to work with.
    if ( ! $content ) {
        return $content;
    }

    // Create an instance of DOMDocument.
    $dom = new \DOMDocument();

    // Supress errors due to malformed HTML.
    // See http://stackoverflow.com/a/17559716/3059883
    $libxml_previous_state = libxml_use_internal_errors( true );

    // Populate $dom with $content, making sure to handle UTF-8.
    // Also, make sure that the doctype and HTML tags are not added to our
    // HTML fragment. http://stackoverflow.com/a/22490902/3059883
    $dom->loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ),
          LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );

    // Restore previous state of libxml_use_internal_errors() now that we're done.
    libxml_use_internal_errors( $libxml_previous_state );

    // Create an instance of DOMXpath.
    $xpath = new \DOMXpath( $dom );

    // Get images then loop through and add additional classes.
    $imgs = $xpath->query( "//img" );
    foreach ( $imgs as $img ) {
        $existing_class = $img->getAttribute( 'class' );
        $img->setAttribute( 'class', "{$existing_class} post-image lazy-load o-image" );
    }

    // Save and return updated HTML.
    $new_content = $dom->saveHTML();
    return $new_content;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.