远方的海
远方的海,WordPress,免费
2015 Aug 21 04 : 07

修改wp_is_mobile()函数判断WordPress手机移动设备

您当前的位置: 首页 > WordPress > 正文
欢迎光临远方的海!您可能还对 WordPress建站免费资源最新IT动态 感兴趣,点击开启 悦读 模式。CTRL+D 快捷收藏本文,我们期待您再次光临!
[文章目录]

WordPress自从3.4版本后就引入了移动判断函数wp_is_mobile(),这个函数在wp-includes文件夹下的vars.php中,我们可以对其修改一下,让它涵盖更多的移动设备检测功能!

wp_is_mobile()原函数

在123行可以找到下面的代码,代码返回的是一个布朗值$is_mobile

function wp_is_mobile() {
 static $is_mobile;
if ( isset($is_mobile) )
 return $is_mobile;
if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
 $is_mobile = false;
 } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
 || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
 || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
 || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
 || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
 || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) {
 $is_mobile = true;
 } else {
 $is_mobile = false;
 }
return $is_mobile;
}

修改版wp_is_mobile()函数

删除原来的wp_is_mobile函数,我们换一个新的,这个就把所有的移动设备比较全的展现出来了!

function wp_is_mobile() {
 $user_agent = $_SERVER['HTTP_USER_AGENT'];
 $mobile_browser = Array(
 "mqqbrowser", //手机QQ浏览器
 "opera mobi", //手机opera
 "juc","iuc",//uc浏览器
 "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",
 "iemobile", "windows ce",//windows phone
 "240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte"
 );
 $is_mobile = false;
 foreach ($mobile_browser as $device) {
 if (stristr($user_agent, $device)) {
 $is_mobile = true;
 break;
 }
 }
 return $is_mobile;
}

函数的调用方法也很简单:

<?php if (wp_is_mobile() ): ?>
//这里写你要实现的代码
<?php endif ;?>

好文!分享给朋友,或者点个赞吧~

文章信息

分类:WordPress

您可能也会喜欢

发表回复

Post Comment