Created
May 20, 2013 13:38
-
-
Save markdcraftww/5612276 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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