Add parameters vimeo videos using wordpress embeds

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=0amp;byline=0amp;portrait=0

so the final result will be :

iframe ... src="http://player.vimeo.com/video/72104173?title=0amp;byline=0amp;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

Topic vimeo oembed Wordpress

Category Web


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 );

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);

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...

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.