最近使用wp插件Smartideo的时候,发现添加bilibili的链接,不出现视频播放器,也没有报错,就度娘了一波,估计是权限不够,需要开通高级功能了,七娃没得关注插件作者的微信扫描的,直接上代码测试,发现网站大佬果不欺我!

打开Smartideo插件smartideo.php文件,

1.在大约87行:array($this, 'smartideo_embed_handler_bilibili') ); 下面加入以下代码:

wp_embed_register_handler( 'smartvideo_bilibili_bv',
    '#https?://www\.bilibili\.com/video/BV(?:(?<video_id1>[a-z0-9_=\-]+)/(?:index_|\#page=)(?<video_id2>[a-z0-9_=\-]+)|(?<video_id>[a-z0-9_=\-]+))#i',
    array($this, 'smartvideo_embed_handler_bilibili_bv') 
);

2.在大约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 );
}

这样就可以直接复制bilibili的网站链接了:https://www.bilibili.com/video/BV1CK4y177mU/

如果你的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 );
}

这样bilibili的所有视频链接都可以用了,不是我原创的,我从度娘取得经验,防止丢失,备份的!

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。