3

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!

  • If you've access to both sites, then I experimented with a self-hosted oEmbed provider in an answer here with a Hello World example. – birgire Aug 30 '15 at 11:36
  • Hi birgire, thanks for commenting. By "both sites", do you mean my wp blog(A) and the video hosting website(B)? By reading through all the links you've sent me, my understanding is that, I make code for oembed support for site B, by providing the endpoint in my own site A? A bit confused about how site A would be able to give a oembed response from site A for site B, and how it all works out. Sorry for the trouble but could you explain very simply what is going on. Thanks. – musasi Aug 31 '15 at 5:39
  • I added an example how you can use this function. PS: you must use @ in front of the username to ping it, like @musasi – birgire Sep 6 '15 at 20:04
3

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!

  • Thanks again, unfortunately I cannot recreate your process, as it only brings up the url, rather than the video embed like your gif shows. Am I missing something? Also, as "Note that here we assume that the all the necessary embed information is contained in the video link.", the link itself does not hold any info related to the embed code, so I don't really get how the call back would work. – musasi Sep 17 '15 at 9:16
  • Note that when I tested it, I didn't use any GET parameters (that's how I constructed the reg-ex), so try to remove ?ap=1 from the url. – birgire Sep 17 '15 at 9:17
  • Got it! I wonder what determines the $matches1-4 in the example, as they print nothing for this particular site.. – musasi Sep 23 '15 at 0:52
  • Would you know how to get this to work in acf/custom fields? It works flawlessly on regular wp posts like your gif did, but only outputs the url in custom fields. – musasi Feb 23 '16 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.