Include GitHub Gists in WordPress content
WordPress.com makes it dead easy to include Gists in posts, but that isn’t available in the WordPress software by default.  So let’s add it!
GitHub’s Gist service normally provides an embed code to include Gists in other web sites. The embed code is really just a script to load the Gist via JavaScript. Since WordPress normally strips content like that (for security purposes), we’ll use a little shortcode snippet to make it easy to reference Gist code.
Here’s that shortcode snippet:
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
<?php | |
// | |
function gist_function( $atts ) { | |
$a = shortcode_atts( array( | |
'url' => 'gist.github.com/Ugotsta/49d45a54022f00060f71' | |
), $atts ); | |
return '<script src="' . esc_attr($a['url']) . '.js"></script>'; | |
} | |
add_shortcode( 'gist', 'gist_function' ); | |
?> |
That snippet can be added to your theme’s functions.php, via a custom plugin or using Code Snippets.
Once added, you’ll be able to add Gist references by url like so:
Please note, we cannot be held accountable if you choose to change that url reference and it blows up your website.