How to make a github gist as Embed

Do you want your gist link to turn directly into code the way WordPress converts a youtube video link directly into a player? Let's see how to do it below.

The example below works in both the visual editor and the block editor.

Use the following code by pasting it into functions.php or create a plugin:

add_action( 'init', 'register_gist_oembed_provider' );

function register_gist_oembed_provider() {

	wp_embed_register_handler(
		'gist',
		'~https://gist\.github\.com/[a-z0-9]+/[a-z0-9]+~im',
		'callback_gist_oembed_provider'
	);
}

function callback_gist_oembed_provider( $matches ) {
	return sprintf( '<script src="%s.js"></script>', $matches[0] );
}

Since with this solution we don't make any queries on the backend, there is no caching of the result.

This Note embeded into: wp_embed_register_handler()