wp_embed_register_handler()
Registers an Embed handler. This is a handler that turns a link in the content into HTML code.
More about oEmbed in WordPress.
This function is designed to create handlers for links (for sites) that do not support the oEmbed format. That is, if the site supports the oEmbed format, then a provider needs to be registered for it, not a custom handler created by this function. The oEmbed provider is registered through the function wp_oembed_add_provider().
No Hooks.
Returns
null. Nothing.
Usage
wp_embed_register_handler( $id, $regex, $callback, $priority );
- $id(string) (required)
- Internal identifier (name) of the handler. Must be unique.
- $regex(string) (required)
- A regular expression against which the URL from the content is matched. $matches is passed to the $callback function.
- $callback(callable) (required)
- The callback function that will be called when the regular expression matches.
- $priority(int)
- Used to specify the order in which registered handlers will be checked.
Default: 10
Examples
#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: Since with this solution we don't make any queries on the backend, there is no caching of the result.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] );
}
#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_embedWordPress Embed object.
Changelog
| Since 2.9.0 | Introduced. |
wp_embed_register_handler() wp embed register handler code WP 6.9.1
function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
global $wp_embed;
$wp_embed->register_handler( $id, $regex, $callback, $priority );
} 