Wordpressのwp_oembed_getを使っていて、Youtubeの関連動画を非表示にさせたい場合。
使われるケースは本当にまれだと思うのですが、覚書として[php]
/**
* Yotubeの動画を追加するwp_oembed_getを拡張
*/
function namespace_oembed_youtube_custom( $html, $url, $args ) {
// Yotube動画を判定
if ( !strstr($url, 'youtube.com') )
return $html;
// URLを取得しクエリ文字列を分解
$url_string = parse_url($url, PHP_URL_QUERY);
parse_str($url_string, $id);
// デフォルトの配列をセット
$defaults = array(
'width' = 480,
'height' = 385,
'showinfo' = true,
'rel' = true
);
// デフォルト値とマージした連想配列を取得
$args = wp_parse_args( $args, $defaults );
// 配列を変数に変換
extract( $args, EXTR_SKIP );
// 独自のパラーメーター出力時に追加
if ( isset($id['v']) ) {
return '<iframe width="' . intval($width) . '" height="' . intval($height) . '" src="http://www.youtube.com/embed/' . $id['v'] . '?rel=' . intval($rel) . '&showinfo=' . intval($showinfo) . '" frameborder="0" allowfullscreen></iframe>';
}
return $html;
}
add_filter('oembed_result', 'namespace_oembed_youtube_custom', 10, 3);
[/php]
で、これの使い方は
[php]
echo wp_oembed_get( 'http://www.youtube.com/watch?v=mx4xeO3Xq7g', array( 'width' = 203, 'height' = 164, 'showinfo' = false, 'rel' = false ) );
[/php]
みたいな感じです。
[php]
'rel' = false
[/php]
ってすれば動画を再生後に関連動画が非表示になります。
ちなみに
[php]
'showinfo' = false
[/php]
の部分で動画のタイトル部分も非表示にできます。
まぁこれは更新機能などでただYotubeの動画のURLを貼ってもらって、
サイトにiFrameで表示する場合に使えると思います。
[php]
'width' => 203, 'height' => 164
[/php]
iFrameの高さと横幅は最初から自由に変えれます。
以上、覚書でした。
コメント