When installing a mobile plugin on a WordPress blog that has W3 Total Cache installed and enabled, you have to address the fact that a page from your mobile theme might be cached and displayed to a desktop user.

W3 Total Cache tried to address this with a section of their plugin that allows the blog owner to specify user agents. What this will do is not served a cached page to the user agent specified and will not cache the view from a mobile device. This is a great first step, but because mobile detection is such an advanced function, this is not a suitable solution. Some mobile plugins have a very limited list of mobile devices they target. So in these cases, the mobile plugin will give you access to a user agent list that you can copy and paste into this box. Recently, the mobile plugins have advanced the functionality of detecting a mobile device which makes this feature of W3 Total Cache obsolete.

To integrate W3TC with the WP Mobile Detector, you must perform the following steps:

  1. Login to the administration panel, click on the “Performance” tab, and then click “User Agent Groups”.
  2. Make sure that the groups “high” and “low” are present, enabled, and set to pass-through.
  3. Open an FTP or your favorite editor and edit the “wp-content/plugins/w3-total-cache/lib/W3/Mobile.php” file.
  4. UPDATE! For version 0.9.2.8+, find the “group_verifier()” function and replace it with the function below.
  5. For all other versions, find the “get_group()” function and replace it with the proper function below.

As you can see from this function, it essentially uses the WP Mobile Detector to determine if it was a mobile device. Smart phones return the “high” group and feature phones return the “low” group.

For Version 0.9.2.8+

function group_verifier($group_compare_value) {
if(function_exists(‘websitez_get_mobile_device’)){ //Check to see if the WP Mobile Detector is installed
$mobile_device = websitez_get_mobile_device(); //Returns an array with mobile detection values
if(is_array($mobile_device) && $mobile_device['type'] == “1″ || is_array($mobile_device) && $mobile_device['type'] == “2″){
return true;
}
} //End WP Mobile Detector hook

return isset($_SERVER['HTTP_USER_AGENT']) && preg_match(‘~’ . $group_compare_value . ‘~i’, $_SERVER['HTTP_USER_AGENT']);
}

For Versions Below 0.9.2.8

/**
* Detects mobile group
*
* @return string
*/
function get_group()
{
static $mobile_group = null;

if(function_exists(‘websitez_get_mobile_device’)){ //Check to see if the WP Mobile Detector is installed
$mobile_device = websitez_get_mobile_device(); //Returns an array with mobile detection values
if(is_array($mobile_device) && $mobile_device['type'] == “1″ && array_key_exists(‘high’, $this->groups) && $this->groups["high"]["enabled"]){
return “high”; //Smart phone device
}else if(is_array($mobile_device) && $mobile_device['type'] == “2″ && array_key_exists(‘low’, $this->groups) && $this->groups["low"]["enabled"]){
return “low”; //Feature phone device
}
} //End WP Mobile Detector hook

if ($mobile_group === null) {
foreach ($this->groups as $group => $config) {
if (isset($config['enabled']) && $config['enabled'] && isset($config['agents'])) {
foreach ((array) $config['agents'] as $agent) {
if ($agent && isset($_SERVER['HTTP_USER_AGENT']) && preg_match(‘~’ . $agent . ‘~i’, $_SERVER['HTTP_USER_AGENT'])) {
$mobile_group = $group;

return $mobile_group;
}
}
}
}

$mobile_group = false;
}

return $mobile_group;
}

This allows the WP Mobile Detector to integrate flawlessly with the W3TC plugin!