最近很多小伙伴在群里讨论如何给网站插入外链视频,正好今天在水煮鱼博客看到一篇教程,使用 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 则会自动转换视频地址。