How to add new embed handler not supported by oembed

Registering a custom embed handler

Here’s an example how we can use the wp_embed_register_handler() function in your case:

/**
 * Register an embed handler for myvi videos
 */
add_action( 'init', function()
{
    wp_embed_register_handler( 
        'myvi', 
        '#http://www\.myvi\.ru/watch/([a-zA-Z0-9_-]+)$#i',   // <-- Adjust this to your needs!
        'myvi_embed_handler' 
    );
} );

Here we constructed the regular expression in a way that doesn’t support any GET parameters. When testing this you therefore need to remove the ?api=1 part from the url. Otherwise you can just adjust the regular expression further to your needs.

The custom callback handler is defined as:

/**
 * Handler callback for the myvi video provider
 */
function myvi_embed_handler( $matches, $attr, $url, $rawattr )
{
    $embed = sprintf(
        '<iframe src="http://myvi.ru/player/embed/html/%1$s" width="600" height="400" frameborder="0" allowfullscreen></iframe>',
        esc_attr( $matches[1] )
    );
    return apply_filters( 'myvi_embed_handler', $embed, $matches, $attr, $url, $rawattr );
}

Note that here we assume that the all the necessary embed information is contained in the video link.

Here’s how it will work in the editor:

testing myvi

You should only do this for sites you really trust!

Leave a Comment