Skip to content

Instantly share code, notes, and snippets.

@roborourke
Created June 13, 2012 22:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save roborourke/2926827 to your computer and use it in GitHub Desktop.
Save roborourke/2926827 to your computer and use it in GitHub Desktop.
Adds autoembedding of gist urls in WordPress
<?php
/**
* 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
*/
wp_embed_register_handler( 'gist', '/https?:\/\/gist\.github\.com\/([a-z0-9]+)(\?file=.*)?/i', 'wp_embed_handler_gist' );
function wp_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 );
}
?>
@braddalton
Copy link

Whats the benefit of adding this code to your functions file? You can still embed gists in WordPress without it?

@diije
Copy link

diije commented Jun 18, 2013

Thanks for this gist !
I updated it so that it takes usernames in URLs into account. See https://gist.github.com/diije/5805069 ;)

@schuhwerk
Copy link

Really helped me too!
Changed diije's Version a bit, so it works with and without usernames: https://gist.github.com/tivus/7201507
So
https://gist.github.com/sanchothefat/2926827
and also
https://gist.github.com/2926827
will work.

@paaljoachim
Copy link

What about filling out a trac ticket adding the newest code so we can have it added directly into WordPress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment