-1

I realize this question has been asked before as I've gone through over 10 similar questions, but I'm trying to figure out what I'm doing wrong. I keep getting the error Warning: DOMDocument::LoadHTML: Empty String Supplied as input. This is frustrating because I'm even getting this error using extremely simple expressions (often generic, straight from forums without editing) and I realize it's due to the fact that I'm new to php and DOMDocument functions.

Here's an example function that returns the error:

function breadcrumb_json() {
    $dom = new DOMDocument();
    $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($html);
    // 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' );
        echo $existing_class;
    }
}

add_action('wp_footer', 'breadcrumb_json');

You can probably see from my clever function name that I'm eventually attempting to create JSON+LD structured data for breadcrumbs on my pages. The bottom line is, no matter how I try this function, I keep getting the empty string error. Does my $html variable need to be replaced with something specific from my site? Is there some global variable I need that I'm missing? This is in Wordpress (ver. 4.8.2) in case that's relevant.

Thanks for the nudge in the right direction!

TomM0419
  • 29
  • 6

1 Answers1

0

Your $html variable is undefined.

PHP does not have lexical scope like Javascript has, so anything you define outside of your breadcrumb_json() function is not known inside the function.

You need to pass $html to breadcrumb_json($html).

Gordon
  • 312,688
  • 75
  • 539
  • 559