Home Blog Embed Github Gists With PHP (or in WordPress with Shortcode)

Embed Github Gists With PHP (or in WordPress with Shortcode)

Github WordPress Shortcode

GitHub is a Web-based Git repository hosting service that offers the typical distributed revision control and source code management functionality of Git, as well as some of its own features. The site has excelled with the the web-based graphical interface, desktop, and mobile integration, in addition to offering the traditional command line tools. The service provides excellent access control, collaboration features (such as bug tracking, feature requests, task management, and wikis for every project).

GitHub also operates a “pastebin-style” service called Gist that is used for hosting code snippets. The service builds upon the simple and traditional pastebin-style repositories littering the web by adding version control for code and easy forking. Because each “gist” has its own Git repository, multiple code snippets can be contained in a single paste and they can be pushed and pulled using Git. Further, forked code can be pushed back to the original author in the form of a patch, so gists (the Github term for the more traditional pastes) can become more like mini-projects.

Github has a number of subscription options of which the most basic is free. Anonymous code can be pasted, but the code cannot be deleted once created.

This article will provide you with very simple shortcode that will embed Github Gists into your WordPress post or page with shortcode. A PHP function to be used outside of WordPress is also provided.

The Result

The shortcode of either [gist id="https://gist.github.com/martinkhoury/dcaf0b7f2d76231d8c6d"] or just [gist id="dcaf0b7f2d76231d8c6d"] will return the following code (an older version of the code shared in this article). View the (secret) snippet on Github here .

Shortcode Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin. You may optionally download and install our plugin from the bottom of of the page.

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes


The ID can be either the full Gist URL or just the Gist ID. For example, you may use [gist id="https://gist.github.com/martinkhoury/98689469889fc449eba8"] or just [gist id="98689469889fc449eba8"].

PHP Function

Used outside of WordPress, the following function can be used.


The shortcode is extremely simple. However, embed code should be avoided when pasting anything into a website – shortcode is almost always a better idea. If the embed code format changes, of you choose to alter formatting, a simple alteration in your code effects changes on your entire website.

While I have no plans of activity on Github, you can follow me on Github here .


Title: Embed Github Gists With PHP (or in WordPress with Shortcode)
Description: Embed Github Gists With PHP (or in WordPress with Shortcode).
Download: Shortcode (V0.2) | WP Plugin (V0.2) | PHP Code (V0.2)

■ ■ ■

Interested in WordPress and Web Design? Subscribe to our list and we'll send you subscriber-only tools and plugins

Short URL for this post: http://shor.tt/19nP


Please enter your comment!
Please enter your name here