Created
July 6, 2020 12:37
-
-
Save sabrina-zeidan/c0684382b634cd91efe458afe91873b7 to your computer and use it in GitHub Desktop.
Converts links to https://gist.github.com in Gutenberg into embeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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