Created
May 8, 2015 15:34
-
-
Save ihorvorotnov/4dc329ad01716ccbd4d0 to your computer and use it in GitHub Desktop.
Custom function to separate phones and tablets with wp_is_mobile() function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* It's just an example, proof of concept. | |
* You should modify it to include more tablets, not only iPad. | |
*/ | |
function my_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'], '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 ) { | |
$is_mobile = true; | |
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') == false) { | |
$is_mobile = true; | |
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'iPad') !== false) { | |
$is_mobile = false; | |
} else { | |
$is_mobile = false; | |
} | |
return $is_mobile; | |
} |
Just for a reference, here's original function in WP core:
https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/vars.php#L0
for anyone who is looking to get this done in 2023:
$tablet_browser = 0;
$mobile_browser = 0;
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$tablet_browser++;
}
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda ','xda-');
if (in_array($mobile_ua,$mobile_agents)) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini') > 0) {
$mobile_browser++;
//Check for tablets on opera mini alternative headers
$stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
$tablet_browser++;
}
}
if ($tablet_browser > 0) {
// do something for tablet devices
print 'is tablet';
}
else if ($mobile_browser > 0) {
// do something for mobile devices
print 'is mobile';
}
else {
// do something for everything else
print 'is desktop';
}
?>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also Mobble plugin:
https://wordpress.org/plugins/mobble/
It gives you some useful functions: