Last active
January 4, 2019 07:14
-
-
Save micc83/5964192 to your computer and use it in GitHub Desktop.
WordPress Gists embed Shortcode
This file contains 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
<?php | |
/** | |
* WordPress Gists Shortcode | |
* | |
* To include a Gist you have different possibilities: | |
* [gists id="5964192"] | |
* [gists]https://gist.github.com/micc83/5964192[/gists] | |
* [gists id="5964192" file="index.html"] | |
* [gists file="index.html"]https://gist.github.com/micc83/5964192[/gists] | |
* [gists https://gist.github.com/micc83/5964192 /] | |
* [gists https://gist.github.com/micc83/5964192 file="index.html /] | |
* | |
* @var Array|String $atts Shortcode attributes | |
* @var String $content Shortcode content | |
* | |
* @return String Gists embed script | |
*/ | |
function minimamente_gists( $atts, $content = null ) { | |
extract( shortcode_atts( array( | |
'id' => '', | |
'file' => '' | |
), $atts ) ); | |
$gists_url = 'https://gist.github.com/'; | |
if ( empty( $content ) && isset( $atts[0] ) ) | |
$content = $atts[0]; | |
if ( !empty ( $content ) ) { | |
$url_parts = explode( '#', $content ); | |
$url = $url_parts[0] . '.js'; | |
} elseif ( ! empty( $id ) ) { | |
$url = $gists_url . $id . '.js'; | |
} else { | |
return false; | |
} | |
if ( !empty( $file ) ) | |
$url = add_query_arg( array( 'file' => $file ), $url ); | |
if ( ( ! $url = esc_url( $url ) ) || 0 !== stripos( $url, $gists_url ) ) | |
return false; | |
return '<script src="'. $url .'"></script>'; | |
} | |
add_shortcode( 'gists', 'minimamente_gists' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment