Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Embed a Gist in Wordpress
<?php
/*
Plugin Name: Gistson - Embedded Gist WP Plugin
Plugin URI: http://arin.me/blog/tag/gistson
Description: Use a shortcode [gist id="12345"] to embed A Gist from http://gist.github.com into your blog
Version: 0.1
Author: Arin Sarkissian
Author URI: http://arin.me
Copyright 2009 Arin Sarkissian
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
CREDIT:
Heavily based on Paul William's plugin:
http://www.entropytheblog.com/blog/
http://www.entropytheblog.com/blog/2008/12/wordpress-github-gist-shortcode-plugin/
Main difference is that this version doesn't do a JS, <script>, embed... the code from your gist is
actually in the HTML source.
INSTALL:
Toss the gistson.php file into your blogs wp-content/plugins folder. Login to WP and enable the plugin.
USE:
Put this <LINK> tag in <HEAD> of header.php
<link rel="stylesheet" href="http://gist.github.com/stylesheets/gist/embed.css"/>
When you wanna embed a gist just type in:
[gist id="gist-id-from-gist.github.com-here"]
example:
[gist id="250709"]
You can exclude the attribution by doing this:
[gist id="250709" nometa="true"]
This is useful for when you have multiple gists. But for big chunks of code etc
I'd encourge you to keep the attribution cuz those guys have a business to run
*/
function gist_shortcode_func($atts, $content = null) {
$url = 'http://gist.github.com/' . trim($atts['id']) . '.json';
$json = file_get_contents($url);
$assoc = json_decode($json, true);
if (isset($atts['nometa'])) {
// you'll end up with 2 1px borders at the bottom =(
$assoc['div'] = preg_replace('/<div class="gist\-meta">.*?(<\/div>)/is', '', $assoc['div']);
}
return $assoc['div'];
}
add_shortcode('gist', 'gist_shortcode_func');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment