Skip to content

Instantly share code, notes, and snippets.

@micc83
Last active January 4, 2019 07:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micc83/5964192 to your computer and use it in GitHub Desktop.
Save micc83/5964192 to your computer and use it in GitHub Desktop.
WordPress Gists embed Shortcode
<?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