More Fun with User Agent Strings and WordPress Conditional Tags for Mobile Devices

I’ve written before on how to create simple conditional tags for WordPress such as is_iphone() & is_ipad(). With Android gadgets ever climbing the plethora of charts claiming browser market share, though, I thought it might be nice to include those guys in the mix. Maybe something will come of this Google company after all.

Thusly, I present to you the following for your functions.php file, a plethora of WordPress functions to add new conditional tags for iOS devices as well as Android, and some fun combos.

// add conditional statements
function is_ipad() { // if the user is on an iPad
$is_ipad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
if ($is_ipad)
return true;
else return false;
}
function is_iphone() { // if the user is on an iPhone
$cn_is_iphone = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPhone');
if ($cn_is_iphone)
return true;
else return false;
}
function is_ipod() { // if the user is on an iPod Touch
$cn_is_iphone = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPod');
if ($cn_is_iphone)
return true;
else return false;
}
function is_ios() { // if the user is on any iOS Device
if (is_iphone() || is_ipad() || is_ipod())
return true;
else return false;
}
function is_android() { // detect ALL android devices
$is_android = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Android');
if ($is_android)
return true;
else return false;
}
function is_android_mobile() { // detect only Android phones
$is_android = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Android');
$is_android_m = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');
if ($is_android && $is_android_m)
return true;
else return false;
}
function is_android_tablet() { // detect only Android tablets
if (is_android() && !is_android_mobile())
return true;
else return false;
}
function is_mobile_device() { // detect Android Phones, iPhone or iPod
if (is_android_mobile() || is_iphone() || is_ipod())
return true;
else return false;
}
function is_tablet() { // detect Android Tablets and iPads
if ((is_android() && !is_android_mobile()) || is_ipad())
return true;
else return false;
}

Up Next: Introducing Ply: An HTML5, CSS3, Responsive Framework