wp_embed_register_handler()WP 2.9.0

Registers an embed handler.

Should probably only be used for sites that do not support oEmbed.

No Hooks.

Return

null. Nothing (null).

Usage

wp_embed_register_handler( $id, $regex, $callback, $priority );
$id(string) (required)
An internal ID/name for the handler. Needs to be unique.
$regex(string) (required)
The regex that will be used to see if this handler should be used for a URL.
$callback(callable) (required)
The callback function that will be called if the regex is matched.
$priority(int)
Used to specify the order in which the registered handlers will be tested.
Default: 10

Examples

0

#1 Embedding gist in content posts

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.

0

#2 Embed handler for Forbes video embedding

wp_embed_register_handler(
	'forbes',
	'~http://(?:www|video)\.forbes\.com/(?:video/embed/embed\.html|embedvideo/)\?show=(?P<show>[\d]+)&format=frame&height=(?P<height>[\d]+)&width=(?P<width>[\d]+)&video=(?P<video>.+?)($|&)~i',
	'wpdocs_embed_handler_forbes'
);

function wpdocs_embed_handler_forbes( $matches, $attr, $url, $rawattr ) {

	$src = add_query_arg(
		[
			'show'   => (int) $matches['show'],
			'format' => 'frame',
			'width'  => (int) $matches['height'],
			'height' => (int) $matches['width'],
			'video'  => esc_attr( $matches['video'] ),
			'mode'   => 'render',
		],
		'http://www.forbes.com/video/embed/embed.html'
	);

	$embed = strtr(
		'<iframe src="'. $src .'" width="{WIDTH}px" height="{HEIGHT}px" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>',
		[
			'{HEIGHT}' => (int) $matches['height'],
			'{WIDTH}'  => (int) $matches['width'],
		]
	);

	return $embed;
}

Notes

  • Global. WP_Embed. $wp_embed

Changelog

Since 2.9.0 Introduced.

wp_embed_register_handler() code WP 6.5.2

function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
	global $wp_embed;
	$wp_embed->register_handler( $id, $regex, $callback, $priority );
}