Resolving GistPress loadHTML error

I use GistPress for embedding gists on this site. I prefer GistPress over the block editor’s built-in functionality of embedding a gist because GistPress does not use Javascript to embed. It also provides enhanced functionality such as limiting which lines to display and caching.

After a recent update, I starting getting PHP warnings:

Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 12 in /public_html/wp-content/plugins/gistpress-develop/includes/class-gistpress.php on line 466

After some searching, I found the answer on Stack Overflow.

There are 2 instances of loadHTML in class-gistpress.php:

  1. Line 466:
    $dom->loadHTML( $html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED );
  2. Line 558:
    $dom->loadHTML( $html );

Add the following directly before each instance:

libxml_use_internal_errors( true );

And the following directly after each instance:

libxml_use_internal_errors( false );

Unfortunately, any updates to the plugin will overwrite these changes, but the plugin is stable and has not been updated for a couple of years.

I hope this helps someone else out there.