Photo of David Winter

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:

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

        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.