本文我們來講講在wordpress中快速插入優酷視頻mbed方式改為 iframe,這個方式的好處是可以兼容移動設備瀏覽,一舉兩得。
現在的 WordPress 中文版內置了“使用 WordPress 的 Embed 功能快速插入優酷視頻”的功能:只要在寫文章時,獨占一行粘貼優酷視頻地址就可以自動調用次視頻。
例如:我在下一行獨占一行插入 http://v.youku.com/v_show/id_XNTgxNzI3MDcy.html 這個優酷視頻地址,就可以自動調用優酷視頻了。
但是這是用 Embed 的,PC端沒問題,手機瀏覽器因為不支持 Flash 所以是無法顯示和播放視頻的,而現在提供的 iframe 方式插入的視頻就支持手機.
下面說說如何實現:
使用 WordPress 的 Embed 功能快速插入優酷視頻,改為以 iframe 方式調用的方法,有點繞口.
1. 確定所用 WordPress 是中文版(好像其他語言版本也行,如果有錯誤提示就把 wp_embed_unregister_handler( 'youku' ); 刪除即可)
2. 就是把下面的代碼扔進所用主題的 functions.php 里面,注意:代碼要放在 <?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 ( !emptyempty($rawattr['width']) && !emptyempty($rawattr['height']) ) {
- $width = (int) $rawattr['width'];
- $height = (int) $rawattr['height'];
- } else {
- list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] ); //Vevb.com
- }
- $iframe = '<iframe width='. esc_attr($width) .' height='. esc_attr($height) .' src="http://player.youku.com/embed/'. esc_attr($matches[1]) .'" frameborder=0 allowfullscreen></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 里面的代碼都要放在 <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 ( !emptyempty($rawattr['width']) && !emptyempty($rawattr['height']) ) {
- $width = (int) $rawattr['width'];
- $height = (int) $rawattr['height'];
- } else {
- list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
- }
- $embed = sprintf(
- '<embed src="http://player.youku.com/player.php/sid/%1$s/v.swf" allowFullScreen="true" quality="high" width="'. esc_attr($width) .'" height="'. esc_attr($height) .'" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>',
- 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_(?<video_id>[a-z0-9_=-]+)#i',
- '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 ( !emptyempty($rawattr['width']) && !emptyempty($rawattr['height']) ) {
- $width = (int) $rawattr['width'];
- $height = (int) $rawattr['height'];
- } else {
- list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
- }
- $iframe = '<iframe width='. esc_attr($width) .' height='. esc_attr($height) .' src="http://player.youku.com/embed/'. esc_attr($matches[1]) .'" frameborder=0 allowfullscreen></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 ( !emptyempty($rawattr['width']) && !emptyempty($rawattr['height']) ) {
- $width = (int) $rawattr['width'];
- $height = (int) $rawattr['height'];
- } else {
- list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
- }
- $embed = sprintf(
- '<embed src="http://player.youku.com/player.php/sid/%1$s/v.swf" allowFullScreen="true" quality="high" width="'. esc_attr($width) .'" height="'. esc_attr($height) .'" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"></embed>',
- 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_(?<video_id>[a-z0-9_=-]+)#i',
- '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 ( !emptyempty($rawattr['width']) && !emptyempty($rawattr['height']) ) {
- $width = (int) $rawattr['width'];
- $height = (int) $rawattr['height'];
- } else {
- list( $width, $height ) = wp_expand_dimensions( 480, 400, $attr['width'], $attr['height'] );
- }
- $iframe = '<iframe width='. esc_attr($width) .' height='. esc_attr($height) .' src="http://player.youku.com/embed/'. esc_attr($matches[1]) .'" frameborder=0 allowfullscreen></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到很多人都不用了轉用其他偏門或者小型的博客系統.
新聞熱點
疑難解答
圖片精選