Skip to content

Instantly share code, notes, and snippets.

@iykex
Forked from bhwebworks/Embed Gists with a URL
Created May 20, 2018 22:53
Show Gist options
  • Save iykex/8b926aff9a85a070cbf5f9130b7ca0ff to your computer and use it in GitHub Desktop.
Save iykex/8b926aff9a85a070cbf5f9130b7ca0ff to your computer and use it in GitHub Desktop.
Add to functions.php or a mu-plugin
/**
* 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/2926827
*
* If a gist has multiple files you can select one using a url in the following format:
* https://gist.github.com/2926827?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', 'bhww_embed_handler_gist' );
function bhww_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