Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Automatically Embed GitHub Gists in WordPress


PHP, WordPress
David Egan

To embed GitHub Gists in WordPress by simply pasting the Gist URL on a new line in the WordPress visual editor, register a handler on the wp_embed_register_handler hook.

Register Gists for Oembed Functionality

<?php
wp_embed_register_handler( 'gist', '#https?://gist.github.com/.*#i', function($url) {
    $url = $url[0];
    if( !preg_match( '#\.js$#i', $url ) )
        $url .= '.js';
    return sprintf('<script src="%s"></script>', esc_attr($url));
});

comments powered by Disqus