How to add new embed handler not supported by oembed

The question:

I am looking to embed videos from websites that do not offer oembed support, but the reference offered on wp codex about wp_embed_register_handler() is just too broad for someone like me.

I tried uploading the example code into my themes functions.php, which I thought would embed any forbes video from just the url, like oembed supported websites like youtube, but only outputs the link onto the post.

(note: all links are edited due to my low rep.)

In my case, I’m trying to add support for a website called myvi.ru.
I would like to paste just the url into a post, and automatically make it output the embed iframe code.

For example, put

http:/www.myvi.ru/watch/Krishtianu-Ronaldu-na-ploschadi-Madrida_I8fWfbDia0ufHwP7W85ZBg2?ap=1

subbing // to /

this into a post, instead having to get the actual embed code, which is this

iframe width="640" height="390" src="/myvi.ru/player/embed/html/o7wvdeXPclZHaazh9Rum_ZuqOtTxR2_ntg5OVhDEhxabugy0qLQDKTbQ0SRijDi8N0" frameborder="0" allowfullscreen></iframe

<> taken out

I thought putting the following into the themes function.php would solve it, but no luck.

wp_embed_register_handler( 'myvi', '#http://(?:www).myvi.ru/(?:video/embed/embed.html|embedvideo/)?show=([d]+)&format=frame&height=([d]+)&width=([d]+)&video=(.+?)($|&)#i', 'wp_embed_handler_myvi' );

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

    $embed = sprintf(
            '<iframe src="http:/www.myvi.ru/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" allowfullscreen></iframe>',
            esc_attr($matches[1]),
            esc_attr($matches[2]),
            esc_attr($matches[3]),
            esc_attr($matches[4])
            );

    return apply_filters( 'embed_myvi', $embed, $matches, $attr, $url, $rawattr );
}

Could anyone please point me in the right direction? I’ve tried googling for other examples to learn from, but cannot find any, and am stuck. I would much appreciate any advice or links to any info that can help. Thanks!

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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="https://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!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment