Skip to content

Instantly share code, notes, and snippets.

@michaelmusgrove
Created March 22, 2013 03:57
Show Gist options
  • Save michaelmusgrove/5218857 to your computer and use it in GitHub Desktop.
Save michaelmusgrove/5218857 to your computer and use it in GitHub Desktop.
Create a Shortcode in WordPress to display Gists by ID. (Example: [github-gist id="GIST-ID"])
add_shortcode('github-gist', 'pu_embed_gist');
function pu_embed_gist($atts, $content = null)
{
extract(shortcode_atts(array('id' => null), $atts));
if( $id != null)
{
$args = array('sslverify' => false);
$result = wp_remote_get('https://api.github.com/gists/' . $id, $args);
$json = json_decode($result['body'], true);
if(isset($json['description']))
{
$description = $json['description'];
if(isset($json['files']['gistfile1.txt']['content']))
{
$content = $json['files']['gistfile1.txt']['content'];
echo '
<h2>'.$description.'</h2>
';
echo '
<pre><code>';
echo $content;
echo '</code></pre>
';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment