Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.