Skip to content

Instantly share code, notes, and snippets.

@topdown
Created July 5, 2012 04:44
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 topdown/3051438 to your computer and use it in GitHub Desktop.
Save topdown/3051438 to your computer and use it in GitHub Desktop.
Gist URL in WP Post
<?php
/**
* This allows you to simply post a gist url in a WordPress 3.0+ post and it grabs the actual gist and displays it
* For example, if your having issues or don't allow posting of embedded javascript, which should not be allowed
*
* Put this code in your theme functions file
* then post a gist url on a new line
*
* https://gist.github.com/1697338
*
*/
wp_embed_register_handler( 'gist', '#https://gist.github.com/([a-zA-Z0-9]+)(\#file_(.+))?$#i', 'wp_embed_handler_gist' );
/**
* @param array $matches
* @param array $attr
* @param string $url
* @param $rawattr
*
* @return mixed|void
* @see http://codex.wordpress.org/Function_Reference/wp_embed_register_handler
*/
function wp_embed_handler_gist( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<script src="https://gist.github.com/%s.js%s"></script>',
esc_attr($matches[1]), '');
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