最近很多小夥伴在群裡討論如何給網站插入外鏈影片,正好今天在水煮魚部落格看到一篇教程,使用 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 則會自動轉換影片地址。