wordpress手机打开网站跳转到指定的页面

 

首先在functions.php中添加如下代码,用is_mobile()函数来判断USER_AGENT。

function is_mobile() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_browser = Array(
"mqqbrowser","opera mobi","juc","iuc","fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod","iemobile","windows ce","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;
}

在header.php的<head></head>标签中添加如下代码进行跳转。

<?php if (is_mobile() ): ?>
<?php 
header("Location: http://m.baddb.com"); 
exit;
?> 
<?php endif ;?>
点赞

发表评论