Add a GitHub Gist to your WordPress site with this nifty Shortcode

Update: This no longer works. There’s a better way now. WordPress embeds allows you to simply copy the gist url into your post, and it will do the rest.

I find that I tend, with the nature of what I blog about, to need to post code into my blogposts. Unfortunately this is one area where the WordPress wysiwyg is lacking. The code tags don’t always work, especially without the pre tags, and sometimes it still strips out important pieces. When you’re blogging about code, you don’t want your code to be messed up.

Github offers a fun feature called gists. It’s not a full git repository, just a way to host a single file or a few lines of code. In addition they offer a script to embed your gist into your site. The only problem is that if you toggle in between the “Visual” and HTML tabs on the WordPress wysiwyg it will probably strip out your script. So, back to the drawing board, right? No.

Add the following code to your theme’s functions.php file and you’re good to go.

I did find a plugin that offers this functionality; however, when I downloaded it and opened it up it had WAY too many lines of code. I appreciate extra functionality and maybe a plugin is the way to go for you if that’s what you need.

But if you just need the shortcode… The above solution is lean and mean.