百度 | 神马 | 搜狗 | 技术文档 | 学习资料分享 - 记录帝国CMS及运维技术的点点滴滴
你的位置:首页 > 实用IT技术 » 正文

【Wordpress相关】WordPress 技巧:使用 Shortcode 方式插入视频,并支持全平台播放

发布时间:2020-09-17

作品分类:Wordpress相关  代码  插入  视频  插入  方式  播放  支持  技巧

WordPress 技巧:使用 Shortcode 方式插入视频,并支持全平台播放,

我们通常使用的优酷和土豆嵌入的代码是 Flash 代码,而 iOS 系统是不会不支持 Flash,所以很多 WordPress 用户写博客的时候,在插入优酷视频的 Flash 代码之后,都要添加一段话,比如:iOS 用户请点该链接查看,这样用户体验非常差,读者看视频也非常不方便。

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>

而优酷和土豆本身已经提供了支持全平台的嵌入代码,我就写了一个 Shortcode 让你非常方便的在文章中插入优酷视频,并支持全平台播放:

<?php
/*
Plugin Name: 使用 shortcode 方式插入优酷和土豆视频,并支持全平台播放
Plugin URI: http://blog.wpjam.com/m/video-shortcode/ 
Author: Denis
*/
add_shortcode( 'youku', 'wpjam_youku_shortcode' );
function wpjam_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 '<iframe class="wpjam_video" height='.esc_attr($height).' width='.esc_attr($width).' src="http://player.youku.com/embed/'.esc_attr($matches[1]).'" frameborder=0 allowfullscreen></iframe>';
    }
}

add_shortcode( 'tudou', 'wpjam_tudou_shortcode' );
function wpjam_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 '<iframe width='. esc_attr($width) .' height='. esc_attr($height) .' src="http://www.tudou.com/programs/view/html5embed.action?code='. esc_attr($matches[1]) .'" frameborder=0 allowfullscreen></iframe>';
    }
}

然后在撰写文章的时候,使用如下方式插入优酷视频:

[youku]http://v.youku.com/v_show/id_XXXXXXXXXXXXXX.html[/youku]
[tudou]http://www.tudou.com/programs/view/XXXXXXX/[/tudou]

默认情况下,视频是 510 宽,498 高,如果你要把宽度和高度设置为 600X500 的话,使用以下代码:

[youku width="600" height="500"]http://v.youku.com/v_show/id_XXXXXXXXXXX.html[/youku]
[tudou width="600" height="500"]http://www.tudou.com/programs/view/XXXXXXX/[/tudou]

已经集成该代码,已经使用 WPJAM Basic 的用户无需再次添加。


本站推荐使用的主机:,国外主机建议使用

Top