wordpress函数wp_is_mobile()是wordpress 3.4.0版本增加的一个内置函数,wp_is_mobile()函数的作用是检测当前浏览器是否运行在智能手机、平板电脑等移动设备上,返回一个布尔值。目前wp_is_mobile()函数支持Iphone、ipad、android、silk、kindle、BlackBerry、Opera Mini等众多移动设备及浏览器,使用该函数可以帮助开发者更好地制作响应式wordpress主题、独立手机主题或者各类型手机相关的插件。
使用方法
if(wp_is_moblile()){
echo '正在使用移动设备';
}else{
echo '浏览使用的不是移动设备';
}
源代码
学习源代码,可以 更好的了解wordpress,因此大家多看看源代码没有坏处。
function wp_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;
}
/**
* Filters whether the request should be treated as coming from a mobile device or not.
*
* @since 4.9.0
*
* @param bool $is_mobile Whether the request is from a mobile device or not.
*/
return apply_filters( 'wp_is_mobile', $is_mobile );
}






















评论抢沙发