Skip to content

Instantly share code, notes, and snippets.

@huypn
Last active April 4, 2016 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huypn/6bd924af89539957f63e4e17b10d0c6d to your computer and use it in GitHub Desktop.
Save huypn/6bd924af89539957f63e4e17b10d0c6d to your computer and use it in GitHub Desktop.
<?php
/**
* Embed Gists with a URL
*
* Usage:
* Paste a gist link into a blog post or page and it will be embedded eg:
* https://gist.github.com/6bd924af89539957f63e4e17b10d0c6d
*
* If a gist has multiple files you can select one using a url in the following format:
* https://gist.github.com/6bd924af89539957f63e4e17b10d0c6d?file=embed-gist.php
*
* Updated this code on June 14, 2014 to work with new(er) Gist URLs
*/
wp_embed_register_handler( 'gist', '/https?:\/\/gist\.github\.com\/([a-z0-9]+)(\?file=.*)?/i', 'huypham_embed_handler_gist' );
function huypham_embed_handler_gist( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<script src="https://gist.github.com/%1$s.js%2$s"></script>',
esc_attr($matches[1]),
esc_attr($matches[2])
);
return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment