HTML Exception Error
-
Just wanted to report a small issue I’ve had. Basically, the plugin was throwing an error as below:
[03-May-2025 08:16:36 UTC] PHP Fatal error: Uncaught ValueError: DOMDocument::loadHTML(): Argument #1 ($source) must not be empty in /var/www/html/wp-content/plugins/fix-alt-text/inc/Scan.php:532
Stack trace:
#0 /var/www/html/wp-content/plugins/fix-alt-text/inc/Scan.php(532): DOMDocument->loadHTML()
#1 /var/www/html/wp-content/plugins/fix-alt-text/inc/Scan.php(273): FixAltText\Scan::get_from_html()
#2 /var/www/html/wp-content/plugins/fix-alt-text/library/_helpers-library/inc/Scan_Process_Library.php(779): FixAltText\Scan::scan_post()
#3 /var/www/html/wp-content/plugins/fix-alt-text/library/_helpers-library/inc/Scan_Process_Library.php(462): FixAltText\HelpersLibrary\Scan_Process_Library->process()
#4 /var/www/html/wp-content/plugins/fix-alt-text/library/_helpers-library/inc/Scan_Process_Library.php(1536): FixAltText\HelpersLibrary\Scan_Process_Library->handle()
#5 /var/www/html/wp-includes/class-wp-hook.php(324): FixAltText\HelpersLibrary\Scan_Process_Library->maybe_handle()
#6 /var/www/html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#7 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action()
#8 /var/www/html/wp-admin/admin-ajax.php(192): do_action()
#9 {main}
thrown in /var/www/html/wp-content/plugins/fix-alt-text/inc/Scan.php on line 532This was causing the progress to get stuck. Turns out the issue was due to effectively empty html (so for example ” ” (single space) not being detected on some post types. I think this is because the site it’s installed on uses ACF extensively and doesn’t always have main content.
I fixed it by adding a catch to the block as below:public static function get_from_html( string $html, array $args = [] ): array {
// Must have something to scan if ( empty( $args ) ) { return []; } // Require HTML if ( empty( $html ) ) { return []; } // Empty array to hold all links to return $all_references = []; // Create DOM structure so we can reliably grab all img tags $dom = new DOMDocument(); // Silence warnings libxml_use_internal_errors( true ); // Ensure we are using UTF-8 Encoding $html = htmlspecialchars_decode( htmlentities( $html, ENT_COMPAT, 'utf-8', false ) ); try { // Load the URL's content into the DOM $dom->loadHTML( $html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED ); $images = $dom->getElementsByTagName( 'img' ); $image_index = 0; // Ensure we have default values $args['from_post_id'] = $args['from_post_id'] ?? 0; $args['from_post_type'] = $args['from_post_type'] ?? ''; $args['from_where'] = $args['from_where'] ?? ''; $args['from_where_key'] = $args['from_where_key'] ?? ''; // Loop through each <img> tag in the dom and add it to the link array foreach ( $images as $img ) { // Inherit base details from args $ref = $args; // Fixing legacy code from breaking Gutenburg Editor $img->removeAttribute( 'id' ); $ref['image_index'] = $image_index; $ref['image_url'] = $img->getAttribute( 'src' ); $ref['image_alt_text'] = $img->getAttribute( 'alt' ); $all_references[] = new Reference( $ref ); ++ $image_index; } } catch ( \Throwable $e ) { error_log( print_r( $html, 1 ) ); // Deliberately empty. } // Done silencing errors libxml_clear_errors(); //Return the links return $all_references; }Anyway, I know its not really a support issue but wanted to report it so you could hopefully include in the next release of what is a great little plugin! I’m going to leave a 5 star review for you now!
Thanks for all the hard work guys!
Steve
The topic ‘HTML Exception Error’ is closed to new replies.