david winter

Quick and simple Github Gist oEmbed support

WordPress doesn’t come with Github Gist oEmbed support out of the box, which is a shame because sometimes there is nothing quicker than creating a Gist to put in a new tech post by just pasting in the URL.

Well, it is quite simple to drop in with a WordPress hook that I’ve written:

wp_embed_register_handler(
    'github-gist',
    '/^https:\/\/gist\.github\.com\/([^\/]+)\/([^\/]+)/',
    function($matches, $attr, $url, $rawattr) {
        $embed = "<script src="\"{$url}.js\""></script>";

        return apply_filters(
            'embed_github_gist',
            $embed,
            $matches,
            $attr,
            $url,
            $rawattr
        );
});

Add that code to your themes functions.php file and then when you’re using the visual editor, if you paste in any Github Gist URL, it’ll automatically be converted. Here’s a little gif to show it in action!

Github Gist oEmbed demo

October 13, 2014

Questions? Comments? Your 2¢'s? → @davidwinter