Quantcast
Channel: WordPress Experts
Viewing all articles
Browse latest Browse all 33

How to Easily Embed And Share Github Gists On Your WordPress Blog!

$
0
0

Github GistsGithub gists are a great way to create and share code snippets, and many developers are using Github. Github is a popular open source code hosting website, that will use git to allow you to push code on into their repositories. A sister site of Github is Gist which allows you to store code snippets on their website to share with everyone else.

This is great resource for finding code snippets to use in your projects. This is also a great place to store your code snippets, if you have a web development blog Gist provides a great way of sharing your code snippets. Because this is a great place to store your code snippets you should be able to use WordPress to get these snippets from Gist and display them on your site.

Today, I’m exited to share this very handy code snippet which allow you to embed and share Github gists on your blog, simply by pasting the gist url. Paste the following code into your functions.php file. Once done, simply paste the URL of a Github gist into a post or page. The gist will be automatically embedded in your blog.

**
 * 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 );
}

Thanks to Robert O’Rourke for this handy piece of code!

The post How to Easily Embed And Share Github Gists On Your WordPress Blog! appeared first on WordPress Experts.


Viewing all articles
Browse latest Browse all 33

Trending Articles