How To Automatically Embed Your Github Gists On WordPress
If you’re an active Github member, then you just might find today’s featured tutorial very helpful in your blogging tasks. It’s from Robert O’Rourke and what it does is it automatically embeds gists from your Github by pasting the URL into your post or page.
All you have to do is paste the following code below into your functions.php file. After doing so, every time you paste the URL of your gist from Github, the gist will automatically be embedded in your blog post.
** * 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\/(\d+)(\?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 ); }
You can check out the demo from Robert’s post. Enjoy!