这篇文章总字数为:2447 字,有 1 张图存于本站服务器
导语:
最近B站调整了视频链接的ID,由原来的AV开头改为BV开头,这导致WordPres视频播放插件Smartideo无法正常播放视频,原因是Smartideo正则无法获取由数字和大小写字母组成的字符串,以前可以直接复制粘贴视频地址到文章中调用视频,现在不行了,有个新办法可以让AV和BV号共存播放视频。
Smartideo插件
支持播放:优酷、bilibili、ACfun、音悦台、网易视频、芒果TV、搜狐视频、土豆、56、腾讯视频、新浪视频、酷6、华数、乐视、YouTube 等视频网站。
修改方法:
1.在Smartideo插件文件smartideo.php的87行:
找到下面代码段:
array($this, 'smartideo_embed_handler_bilibili') );
下面添加:
class="prettyprint">wp_embed_register_handler( 'smartvideo_bilibili_bv',
'#https?://www\.bilibili\.com/video/BV(?:(?[a-z0-9_=\-]+)/(?:index_|\#page=)(?[a-z0-9_=\-]+)|(?[a-z0-9_=\-]+))#i',
array($this, 'smartvideo_embed_handler_bilibili_bv') );
大约在195行找到代码:
return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
在下面添加:
public function smartvideo_embed_handler_bilibili_bv( $matches, $attr, $url, $rawattr ) {
$matches['video_id'] = ($matches['video_id1'] == '') ? $matches['video_id'] : $matches['video_id1'];
$page = ($matches['video_id2'] > 1) ? $matches['video_id2'] : 1;
$cid = '';
$embed = $this->get_iframe("//player.bilibili.com/player.html?bvid={$matches['video_id']}&cid={$cid}&page={$page}", $url);
return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
若要让Smartideo同时支持播放原来AV开头视频,修改以下代码。
public function smartideo_embed_handler_bilibili( $matches, $attr, $url, $rawattr ) {
$matches['video_id'] = ($matches['video_id1'] == '') ? $matches['video_id'] : $matches['video_id1'];
$page = ($matches['video_id2'] > 1) ? $matches['video_id2'] : 1;
$cid = '';
/*
try{
$request = new WP_Http();
$url = "https://api.bilibili.com/view?type=jsonp&appkey=8e9fc618fbd41e28&id=" . $matches['video_id'];
$data = (array)$request->request($url, array('timeout' => 3));
$json = json_decode($data['body'], true);
$cid = $json['cid'];
}catch(Exception $e){}
*/
if(wp_is_mobile() || $this->bilibili_pc_player == 1){
$embed = $this->get_iframe("//player.bilibili.com/player.html?aid={$matches['video_id']}&cid={$cid}&page={$page}", $url);
}else{
$embed = $this->get_link($url);
}
return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
}
替换上面代码为:
public function smartideo_embed_handler_bilibili( $matches, $attr, $url, $rawattr ) {
$matches['video_id'] = ($matches['video_id1'] == '') ? $matches['video_id'] : $matches['video_id1'];
$page = ($matches['video_id2'] > 1) ? $matches['video_id2'] : 1;
$cid = '';
$embed = $this->get_iframe("//player.bilibili.com/player.html?aid={$matches['video_id']}&cid={$cid}&page={$page}", $url);
return apply_filters( 'embed_bilibili', $embed, $matches, $attr, $url, $rawattr );
}
如果没时间自己修改,可以直接下载下面修改好的Smartideo插件。
插件下载
评论