Embed Gists on your WordPress blog without a plugin

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.

Gist – Embed Gists with a URL

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.

CC BY-NC-SA 4.0 Embed Gists on your WordPress blog without a plugin by 桔子小窝 is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据