最近很多小夥伴在羣裏討論如何給網站插入外鏈視頻,正好今天在水煮魚博客看到一篇教程,使用 WordPress 的短代碼 (Shortcode) 插入優酷網和土豆網的視頻,並且支持全平台播放,代碼如下:
add_shortcode( 'youku', 'wxd_youku_shortcode' );
function wxd_youku_shortcode( $atts, $content='' ) {
extract( shortcode_atts( array(
'width' => '510',
'height' => '498',
), $atts ) );
if(preg_match('#http://v.youku.com/v_show/id_(.*?).html#i',$content,$matches)){
return '<iframe height='.esc_attr($height).' width='.esc_attr($width).' src="http://player.youku.com/embed/'.esc_attr($matches[1]).'" frameborder=0 allowfullscreen></iframe>';
}
}
add_shortcode( 'tudou', 'wxd_tudou_shortcode' );
function wxd_tudou_shortcode( $atts, $content='' ) {
extract( shortcode_atts( array(
'width' => '480',
'height' => '400',
), $atts ) );
if(preg_match('#http://www.tudou.com/programs/view/(.*?)#i',$content,$matches)){
return '<iframe width='. esc_attr($width) .' height='. esc_attr($height) .' src="http://www.tudou.com/programs/view/html5embed.action?code='.%20esc_attr($matches[1])%20.'" frameborder=0 allowfullscreen></iframe>';
}
}
然後在撰寫文章的時候,直接寫上視頻地址,shortcode 則會自動轉換視頻地址。