wp_embed_register_handler( string $id, string $regex, callable $callback, int $priority = 10 )

Registers an embed handler.


Description Description

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


Parameters Parameters

$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) (Optional) Used to specify the order in which the registered handlers will be tested.

Default value: 10


Top ↑

Source Source

File: wp-includes/embed.php

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

Top ↑

Changelog Changelog

Changelog
Version Description
2.9.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Basic Example

    Register an embed handler for Forbes video embeds.

    wp_embed_register_handler(
    	'forbes',
    	'#http://(?:www|video)\.forbes\.com/(?:video/embed/embed\.html|embedvideo/)\?show=([\d]+)&format=frame&height=([\d]+)&width=([\d]+)&video=(.+?)($|&)#i',
    	'wpdocs_embed_handler_forbes'
    );
    
    function wpdocs_embed_handler_forbes( $matches, $attr, $url, $rawattr ) {
    
    	$embed = sprintf(
    			'<iframe src="http://www.forbes.com/video/embed/embed.html?show=%1$s&format=frame&height=%2$s&width=%3$s&video=%4$s&mode=render" width="%3$spx" height="%2$spx" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe>',
    			esc_attr($matches[1]),
    			esc_attr($matches[2]),
    			esc_attr($matches[3]),
    			esc_attr($matches[4])
    			);
    
    	return $embed;
    }
    
  2. Skip to note 2 content
    Contributed by Ryan McCue

    Note that the $regex parameter is checked against the URL, not against the content, so you can anchor the regular expression with ^ and $. This is useful if you want to use an ungreedy match group at the end of your URL:

    wp_embed_register_handler( $id, '#^http://example.com/(\?.+)?$#', __NAMESPACE__ . '\\handle_embed' );
    

You must log in before being able to contribute a note or feedback.