最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress中用 iframe 方式插入优酷视频 兼容移动设备
时间:2022-06-25 18:50:21 编辑:袖梨 来源:一聚教程网
现在的 WordPress 中文版内置了“使用 WordPress 的 Embed 功能快速插入优酷视频”的功能:只要在写文章时,独占一行粘贴优酷视频地址就可以自动调用次视频。
例如:我在下一行独占一行插入 http://v.youku.com/v_show/id_XNTgxNzI3MDcy.html 这个优酷视频地址,就可以自动调用优酷视频了。
但是这是用 Embed 的,PC端没问题,手机浏览器因为不支持 Flash 所以是无法显示和播放视频的,而现在提供的 iframe 方式插入的视频就支持手机。
下面说说如何实现:
『使用 WordPress 的 Embed 功能快速插入优酷视频』改为以 iframe 方式调用的方法
有点绕口,?濉F涫岛芗虻?br />
1. 确定所用 WordPress 是中文版(好像其他语言版本也行,如果有错误提示就把 wp_embed_unregister_handler( 'youku' ); 删除即可)
2. 就是把下面的代码扔进所用主题的 functions.php 里面(注意:代码要放在 里面)
/**
* 『使用 WordPress 的 Embed 功能快速插入优酷视频』改为以 iframe 方式调用
* by zwwooooo | zww.me
*/
// 移除原来 WordPress 中文版内置的“使用 WordPress 的 Embed 功能快速插入优酷视频”
wp_embed_unregister_handler( 'youku' );
// 改用 iframe 方式
function wp_iframe_handler_youku( $matches, $attr, $url, $rawattr ) {
// If the user supplied a fixed width AND height, use it
if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
$width = (int) $rawattr['width'];
$height'];
} else {
list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
}
$iframe = '';
return apply_filters( 'iframe_youku', $iframe, $matches, $attr, $url, $ramattr );
}
wp_embed_register_handler( 'youku_iframe', '#http://v.youku.com/v_show/id_(.*?).html#i', 'wp_iframe_handler_youku' );
PS:其他视频网站可以照葫芦画瓢改下代码就行了。
PC端embed方式移动端iframe方式快速插入优酷视频两个方法
我共享一下帮一个朋友做主题时写的这功能的代码吧,我用以前收藏的移动端判断函数来实现根据使用环境决定使用 embed 还是 iframe。
懒得具体说明了,直接上代码吧,里面有注释:另外这次的代码我特意修改做了兼容,支持任何语言的 WordPress
代码实现 PC 端 embed 方式移动端 iframe 方式快速插入优酷视频功能
1. 在 functions.php 插入以下代码(有些代码[如判断是否mobile]收录自搜索,源自哪里已无从考证——好吧,我忘了,如果你是原创作者请联系我)
【注:凡是加入主题的 functions.php 里面的代码都要放在
//判断是否移动设备 Modify by zwwooooo | zww.me
function zfunc_is_mobile() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
$is_mobile = false;
foreach ($mobile_agents as $device) {
if (stristr($user_agent, $device)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}
//// 如果 WordPress 不是中文版,增加优酷视频自动插入 Modify by zwwooooo | zww.me
if (get_bloginfo('language') != 'zh-CN') {
function wp_embed_handler_youku( $matches, $attr, $url, $rawattr ) {
// If the user supplied a fixed width AND height, use it
if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
$width = (int) $rawattr['width'];
$height'];
} else {
list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
}
$embed = sprintf(
'',
esc_attr( $matches['video_id'] ) );
return apply_filters( 'embed_youku', $embed, $matches, $attr, $url, $rawattr );
}
wp_embed_register_handler( 'youku',
'#https?://v.youku.com/v_show/id_(?
'wp_embed_handler_youku' );
}
//// 对移动端的视频使用 iframe 方式插入 by zwwooooo | zww.me
if( zfunc_is_mobile() ){
//// 如果 WordPress 是中文版,移除原来的 youku 视频代码自动插入
if (get_bloginfo('language')==='zh-CN')
wp_embed_unregister_handler( 'youku' );
//// 增加 iframe 方式的视频调用
function wp_iframe_handler_youku( $matches, $attr, $url, $rawattr ) {
// If the user supplied a fixed width AND height, use it
if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
$width = (int) $rawattr['width'];
$height'];
} else {
list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
}
$iframe = '';
return apply_filters( 'iframe_youku', $iframe, $matches, $attr, $url, $ramattr );
}
wp_embed_register_handler( 'youku_iframe', '#http://v.youku.com/v_show/id_(.*?).html#i', 'wp_iframe_handler_youku' );
}
2. 用法:直接把优酷视频地址粘贴到文章内容里面,注意要独占一行
-------------
继续玩手机去~最近刷机刷狂了~
哦,我喜欢上那些带 HALO 的 ROM 了……
-------------
在评论中,大发提醒 WordPress 已经内置了移动设备判断函数 wp_is_mobile(),这是 WordPress 3.4 增加的,我没注意,所以代码可以精简成:
//// 如果 WordPress 不是中文版,增加优酷视频自动插入 Modify by zwwooooo | zww.me
if (get_bloginfo('language') != 'zh-CN') {
function wp_embed_handler_youku( $matches, $attr, $url, $rawattr ) {
// If the user supplied a fixed width AND height, use it
if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
$width = (int) $rawattr['width'];
$height'];
} else {
list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
}
$embed = sprintf(
'',
esc_attr( $matches['video_id'] ) );
return apply_filters( 'embed_youku', $embed, $matches, $attr, $url, $rawattr );
}
wp_embed_register_handler( 'youku',
'#https?://v.youku.com/v_show/id_(?
'wp_embed_handler_youku' );
}
//// 对移动端的视频使用 iframe 方式插入 by zwwooooo | zww.me
if( wp_is_mobile() ){
//// 如果 WordPress 是中文版,移除原来的 youku 视频代码自动插入
if (get_bloginfo('language')==='zh-CN')
wp_embed_unregister_handler( 'youku' );
//// 增加 iframe 方式的视频调用
function wp_iframe_handler_youku( $matches, $attr, $url, $rawattr ) {
// If the user supplied a fixed width AND height, use it
if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
$width = (int) $rawattr['width'];
$height'];
} else {
list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
}
$iframe = '';
return apply_filters( 'iframe_youku', $iframe, $matches, $attr, $url, $ramattr );
}
wp_embed_register_handler( 'youku_iframe', '#http://v.youku.com/v_show/id_(.*?).html#i', 'wp_iframe_handler_youku' );
}
OK,WordPress 真是无孔不入啊,牛x到很多人都不用了转用其他偏门或者小型的博客系统
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22