Skip to content

Instantly share code, notes, and snippets.

@sabrina-zeidan
Created July 6, 2020 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabrina-zeidan/c0684382b634cd91efe458afe91873b7 to your computer and use it in GitHub Desktop.
Save sabrina-zeidan/c0684382b634cd91efe458afe91873b7 to your computer and use it in GitHub Desktop.
Converts links to https://gist.github.com in Gutenberg into embeds
//Gist embeds in Gutenberg, quick fix
//Works if you add https://gist.github.com links as regular links
add_filter( 'the_content', 'gist_in_gutenberg' );
function gist_in_gutenberg( $content ) {
$doc = new DOMDocument();
$doc->loadHTML($content);
foreach ($doc->getElementsByTagName('a') as $link){
$href = $link->getAttribute('href');
if (strpos($href, 'gist.github.com') !== false) {
$element = $doc->createElement('script');
$element->setAttribute('src',$href.'.js');
$link->parentNode->appendChild($element);
$links_to_remove[] = $link;
}
}
foreach ($links_to_remove as $link) {
$link->parentNode->removeChild($link);
}
$content = $doc->saveHTML();
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment