指定したユーザの動画をランダムに選んで表示します。YouTubeのAPIを呼び出している所の引数を変えれば、持ってくるデータはいろいろ変えられます。
使うときには、PHPファイルをpluginsディレクトリに入れて有効にしてから、本文に、
[random_youtube author="snaksnaaaaak"]
のようにユーザ名を指定してショートコードとして書きます。
<?php
/*
Plugin Name: Random YouTube
Version: 1.0
Author: Satoshi Nakamura
*/
function random_youtube_handler($atts) {
extract(shortcode_atts(array(
'author' => '',
'width' => 560,
'height' => 340
), $atts));
$html = '';
if ($author) {
$xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos?author=$author&orderby=published");
if ($xml) {
$videos = $xml->entry;
$count = count($videos);
if ($count != 0) {
$video = $videos[rand(0, $count - 1)];
if (preg_match('/[^\/]+$/', $video->id, $matches)) {
$id = $matches[0];
$title = $video->title;
$html = <<< END
<h2>$title</h2>
<p>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="$width"
height="$height"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="allowFullScreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="src" value="http://www.youtube.com/v/$id&hl=ja&fs=1&" />
<param name="allowfullscreen" value="true" />
<embed type="application/x-shockwave-flash"
width="$width"
height="$height"
src="http://www.youtube.com/v/$id&hl=ja&fs=1&"
allowscriptaccess="always"
allowfullscreen="true"></embed>
</object>
</p>
END;
}
}
}
}
return $html;
}
add_shortcode('random_youtube', 'random_youtube_handler');
?>