Advertisement
firoze

Embed Github Gists on your WordPress blog without a plugin

Apr 6th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. // Embed Gists on your WordPress blog without a plugin
  2.  
  3. // source link: http://blackhillswebworks.com/2013/08/03/embed-gists-on-your-wordpress-blog-without-a-plugin/
  4.  
  5. /**
  6. * Embed Gists with a URL
  7. *
  8. * Usage:
  9. * Paste a gist link into a blog post or page and it will be embedded eg:
  10.  
  11. * https://gist.github.com/2926827 // here https://gist.github.com/sanchothefat/2926827 have remove username "sanchothefat"
  12. *
  13. * If a gist has multiple files you can select one using a url in the following format:
  14. * https://gist.github.com/2926827?file=embed-gist.php
  15. *
  16. * Updated this code on June 14, 2014 to work with new(er) Gist URLs
  17. */
  18.  
  19. /*
  20. simply place the following code in your functions.php file or in a mu-plugin.
  21. */
  22.  
  23. wp_embed_register_handler( 'gist', '/https?:\/\/gist\.github\.com\/([a-z0-9]+)(\?file=.*)?/i', 'bhww_embed_handler_gist' );
  24.  
  25. function bhww_embed_handler_gist( $matches, $attr, $url, $rawattr ) {
  26.  
  27. $embed = sprintf(
  28. '<script src="https://gist.github.com/%1$s.js%2$s"></script>',
  29. esc_attr($matches[1]),
  30. esc_attr($matches[2])
  31. );
  32.  
  33. return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr );
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement