1

I am using a custom field to embed any supported video on Wordpress so for example the user enters a video address on the custom field box :

http://vimeo.com/72104173

and I implemented the following code on my theme :

<?php
$videourl = my_meta('video'); // get custom field value
if($videourl!=''){ // if custom field exist  ?>
<?php 
$htmlcode = wp_oembed_get("{$videourl}"); //use oembed  
echo "<div class='video'>{$htmlcode}</div>"; //output the video ?>
...

the result/output is below :

<iframe ... src="http://player.vimeo.com/video/72104173"></iframe>

the problem is that I want to add extra parameters to the vimeo so I can hide the Title and Byline of the video by adding the following to the src :

?title=0&amp;byline=0&amp;portrait=0

so the final result will be :

<iframe ... src="http://player.vimeo.com/video/72104173?title=0&amp;byline=0&amp;portrait=0"></iframe>

I tried to user str_replace but the problem is that the parameters are added to the end of each video src and the surce is going to be different each time,

really appreciate any help, thanks

1

You can add query args to the end of the URL like so:

$videourl = add_query_arg( array('key1' => 'value1' ), $videourl ); // you can add as many as you want in key/value pairs...

$htmlcode = wp_oembed_get( $videourl );
// rest of your code...
  • Thank you for the response, it doesn't seems to be working for me I added $videourl = add_query_arg( array('title' => '0'), $videourl ); – Andy M Aug 19 '13 at 15:23
  • What is not working? You have to be more specific. What do you get when you var dump $videourl? – user23654 Aug 19 '13 at 16:04
  • exactly the same thing as before, the output is still <iframe ... src="http://player.vimeo.com/video/72104173"></iframe> without the premeters – Andy M Aug 19 '13 at 18:53
  • Well, I can't really help you further then because what I suggested works 100%. You should do var dump before the oembed function. It CANNOT be the same as before unless you didn't do it correctly. – user23654 Aug 19 '13 at 19:10
  • this is the exact code I have in my theme : http://pastebin.com/UsED7zGn can you please implement those parameters ? I tried it like few times and the result is still the same as before for me, so it is probably something I am doing wrong, thanks – Andy M Aug 19 '13 at 19:47
0

What I guess (didn't try) doesn't work with the other solution might be that appending to the url before asking Vimeo for the embed code might just be ignored by Vimeo.

So what I do is simply parse the response with a RegEx:

$htmlcode = preg_replace("/(http:\/\/player.vimeo.com\/video\/[0-9]*)/i", '$1?title=0&portrait=0&byline=0&color=ffffff', $htmlcode);
0

There are several filters in WordPress for altering oEmbed data, depending on when you need to modify the results:

  1. embed_handler_html
    Filters the returned embed handler.
  2. embed_oembed_html
    Filters the cached oEmbed HTML.
  3. oembed_result
    Filters the HTML returned by the oEmbed provider.

Choose the one that best suits your needs, but keep in mind that each filter acts on data at a different stage of the oEmbed process and could have performance implications.

For instance, embed_handler_html is called before data is put in the oEmbed / wp_postmeta cache (so only once per external embed) while embed_oembed_html is called after retrieving the oEmbed cache (so every time the item is displayed).

If you only need to modify the oEmbed data once, oembed_result is probably your best candidate.

To answer your question, here's an example that modifies the Vimeo and YouTube embed parameters returned from the WordPress wp_oembed_get() function.

The <iframe> HTML markup is first parsed by PHP's DOMDocument class, then the embed URL is modified using WordPress' add_query_arg() function:

/**
 *
 */
add_filter( 'oembed_result', function ( $html, $url, $args ) {
    $doc = new DOMDocument();
    $doc->loadHTML( $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
    $tags = $doc->getElementsByTagName( 'iframe' );

    foreach ( $tags as $tag ) {
        $iframe_src = $tag->attributes->getNamedItem('src')->value;

        if ( false !== strpos( $iframe_src, 'youtube.com' ) ) {
            // https://developers.google.com/youtube/player_parameters
            $url = add_query_arg( array(
                'autohide' => 1,
                'autoplay' => 1,
                'controls' => 2,
                'feature' => null,
                'modestbranding' => 1,
                'playsinline' => 1,
                'rel' => 0,
                'showinfo' => 0,
            ), $iframe_src );
        }

        if ( false !== strpos( $iframe_src, 'vimeo.com' ) ) {
            // https://developer.vimeo.com/player/embedding
            $url = add_query_arg( array(
                'autoplay' => 1,
                'badge' => 0,
                'byline' => 0,
                'portrait' => 0,
                'title' => 0,
            ), $iframe_src );
        }

        $tag->setAttribute( 'src', $url );

        $html = $doc->saveHTML();
    }

    return $html;
}, 10, 3 );

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.