B站(bilibili)近期调整视频链接 ID 为 BV 开头,B 站说是为了保护稿件信息安全,容纳更多投稿,维护 UP 主的权益。但升级后的链接造成 WordPres 视频播放插件Smartideo 中的正则无法获取由数字和大小写字母组成的字符串,原来直接复制粘贴视频地址到文章中调用视频的方法失效,用户已在插件作者网站反映,作者并没有回应。
打开Smartideo 插件 smartideo.php 文件,在大约 87 行,Begin 主题 inc 目录下大约 45 行
array($this, 'smartideo_embed_handler_bilibili') );
下面添加
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 行,Begin 主题 inc 目录下大约 106 行
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 );
}
之后直接在文章中添加 B 站视频地址
https://www.bilibili.com/video/BV1WW411b7NQ
同时兼容原来的 av 地址修改方法
https://www.bilibili.com/video/av17792443
找到下面代码
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 );
}
修改完成后同时兼容 av 和 BV 号。