Skip to content

Instantly share code, notes, and snippets.

@markdcraftww
Created May 20, 2013 13:38
Show Gist options
  • Save markdcraftww/5612276 to your computer and use it in GitHub Desktop.
Save markdcraftww/5612276 to your computer and use it in GitHub Desktop.
wp_embed_register_handler( 'github-gist', '#https?://gist\.github\.com/([0-9]+)#', 'gist_embed_handler' );
add_shortcode( 'gist', 'gist_shortcode' );
function gist_embed_handler( $matches, $attr, $url, $rawattr ) {
return gist_shortcode( $attr, $url );
}
function gist_shortcode( $atts, $content = '' ) {
if ( empty( $atts[0] ) && empty( $content ) )
return '<!-- Missing Gist ID -->';
$id = ( ! empty( $content ) ) ? $content : $atts[0];
if ( ! is_numeric( $id ) )
$id = preg_replace( '#https?://gist.github.com/([0-9]+)#', '$1', $id );
$id = (int) $id;
if ( ! $id )
return '<!-- Invalid Gist -->';
$embed_url = "https://gist.github.com/{$id}.js";
if ( ! empty( $atts['file'] ) )
$embed_url = add_query_arg( 'file', urlencode( $atts['file'] ), $embed_url );
return '<script src="' . esc_url( $embed_url ) . '"></script>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment