When I first started writing tutorial blog posts that involved posting code snippets I would add the code directly into the posts and use the SyntaxHighlighter Evolved plugin to render it readable on the front end. This sometimes worked, and sometimes didn’t.
Eventually I started using Gists on GitHub as public repositories for my code snippets, and installed a plugin on my site to embed and display the Gists in the blog posts. This worked okay, but it depended on a plugin.
Then I ran across a post by Christian Engvall (who cited Robert O’Rourke) with a way to embed Gists without a plugin, simply by placing the following code in your functions.php file or in a mu-plugin.
Simply add the Gist URL to your post
With that code in place, all you need to do is add the URL of a Gist into your post (or page) on its own line and the Gist will be embedded in the post on the front end.
Remove your username from the Gist URL
Gist adds your username to the Gist URL, and if you leave that username in the URL in your post then it won’t embed the Gist – it will simply display the URL.
Sandeep says
thanks for the post. was really helpful
John Sundberg says
You’re welcome, Sandeep!
Shemul says
I am using oEmbed gist plugin for this. Whats the benefit of not using plugin. I will now try it. It will reduce at least one plugin.
John Sundberg says
Hey Shemul,
That’s pretty much the benefit – one less plugin to update and maintain.
Sarah says
Hey John!
Thanks for such a great tutorial. I was looking for something instead of plugin to embed gists from github. Am happy now i can get rid of atleaste one plugin. Cheers
John Sundberg says
You’re welcome Sarah!
Kingsley says
Nice Tutorial John sounds easy, will give it a try
Kingsley says
tried it but did not work, instead it displayed the gist url
John Sundberg says
Did you see the last paragraph in my blog post? If you leave your username in the URL then it will just display the URL.
John