最近很多小伙伴在群里讨论如何给网站插入外链视频,正好今天在水煮鱼博客看到一篇教程,使用 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 '';
}
}
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 '';
}
}
然后在撰写文章的时候,直接写上视频地址,shortcode 则会自动转换视频地址。