Embed GitHub Gists In Your WordPress Posts Without Using Plugins

Embed GitHub Gists In Your WordPress Posts Without Using Plugins

This WordPress tutorial will show you how to easily embed GitHub Gists into your WordPress posts by placing a small code snippet into your WordPress themes functions.php file. After you insert this code into your thmes functions.php you will be able to embed Github Gists into your WordPress post content by simply pasting the Gists url into your WordPress post editor.


/**
* Usage:
* Paste a gist link into a blog post or page and it will be embedded eg:
* https://gist.github.com/2926827
*
* If a gist has multiple files you can select one using a url in the following format:
* https://gist.github.com/2926827?file=embed-gist.php
*/

wp_embed_register_handler( 'gist', '/https:\/\/gist\.github\.com\/(\d+)(\?file=.*)?/i', 'wp_embed_handler_gist' );

function wp_embed_handler_gist( $matches, $attr, $url, $rawattr ) {

$embed = sprintf(
'<script type="text/javascript" src="https://gist.github.com/%1$s.js%2$s"></script>',
esc_attr($matches[1]),
esc_attr($matches[2])
);

return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr );
}
Tagged: , , , ,

Author:  | Website: http://stuartduff.com/ | Twitter: Follow Stuart

Stuart Duff is the founder of WPShock and works at WooThemes as a support ninja. In his spare time you can find Stuart Developing plugins and themes for WordPress, discussing WordPress or drinking tea.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>