[wp-hackers] Youtube oembed & plugin fix

Fabrizio Gargano f.gargano at consulenti.kataweb.it
Fri Jul 4 14:26:28 UTC 2014


Hi everybody.
It seems youtube.com oembed result for playlists is a bit incorrect.

Example:
http://www.youtube.com/watch?v=N1zxDa3t0fg&list=PLE90C50E033BD1351&feature=share&index=2

The oembed url for xml format is:
http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DN1zxDa3t0fg%26list%3DPLE90C50E033BD1351%26index%3D3&format=xml

The html tag, imho, should have a src attribute for iframe like
"http://www.youtube.com/embed/videoseries?list=PLE90C50E033BD1351&index=1"

Youtube's iframe src lacks the index parameter, so the embed starts to
play from the first video.

So I wrote a simple mu-plugins to append it to html results:

<?php
function fix_iframe_src($html, $url){
    if(strpos($url,"http://www.youtube.com/")!==false &&
strpos($url,"list=")!==false && strpos($html,"index=")===false){
      foreach(explode("&",$url) as $param){
        if(preg_match('/^index=([0-9]+)$/',$param,$result)===1){
           $index=$result[1];
           $index--;
           return preg_replace('/(src="[^"]+)/',"\\1&index=$index",$html);
        }
      }
    }
    return $html;
}
add_filter('embed_oembed_html','fix_iframe_src',10,2);


Regards,
F.


More information about the wp-hackers mailing list