Websitez.com

How To Get W3 Total Cache & Mobile Plugins To Work

Posted January 26, 2011, 3:32 pm in How To


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!

16 Responses to How To Get W3 Total Cache & Mobile Plugins To Work

  1. Gary August 24, 2011

    Thank you for the detailed explanation: I am not very technically astute, but this seems easy enough.

  2. eddie February 10, 2012

    Dont forget to mention that the code replacement has to be done everytime W3TC is upgraded.

  3. eddie February 10, 2012

    I dont understand why I got the same score on ready.mobi before AND after code replacement.

    is this code replacement still necessary with the current version of W3TC?

    thanks

    • Eric Stolz February 11, 2012

      Eddie,

      Did you clear your cache?

  4. eddie February 13, 2012

    yes… check it out
    ready.mobi/results.jsp?uri=http%3A%2F%2Fwww.preserveassim.org%2Ffora-duto%2F&locale=en_EN

  5. Tammy February 5, 2013

    W3TC just had an update & this is no longer valid in the Mobile.php file as far as I can tell. Is this still a concern, or is there something else we should be doing?

    • Eric Stolz February 5, 2013

      What version of W3TC are you using now? 0.9.2.6?

      • Tammy February 6, 2013

        Today’s Version 0.9.2.7 with Mobile Detector Version 1.7.7.

  6. Tammy February 12, 2013

    w3tc is now up to Version 0.9.2.8.

  7. Andy March 3, 2013

    @Eric, I’m seeing the same thing as @Tammy. Can you confirm?

    After upgrading w3tc I did not need to make the changes in mobile.php. The detector “seems” to be working fine without it.

    • Eric Stolz March 23, 2013

      I updated the blog post with a new snippet that needs to be updated in the mobile.php file.

  8. Revery March 12, 2013

    The links is broken, what is the snippet?
    http://snipplr.com/view/70071/wp-mobile-detector–0928/

    • Eric Stolz March 23, 2013

      It works for me?

  9. Yash Bhatia April 3, 2013

    The link for the code doesn’t work. What is the snippet needed to be pasted in the mobile.php file?
    Could you please post it as a comment or an update in the blog post?

    • Eric Stolz April 3, 2013

      Sorry about that, I had it marked as private for some reason. The link should work now.

  10. Tammy April 19, 2013

    (posted before, but if comment is in moderation, sorry for the duplicate!)

    With W3TC Version 0.9.2.9, the above code addition causes my site to crash – any suggestion?
    Site – http://www.goffstownlibrary.com

    Modified Mobile.php looks like this:

    <?php

    /**
    * W3TC Mobile detection
    */

    w3_require_once( W3TC_LIB_W3_DIR . '/CacheCase.php');

    /**
    * Class W3_Mobile
    */
    class W3_Mobile extends W3_CacheCase{
    /**
    * PHP5-style constructor
    */
    function __construct() {
    parent::__construct('mobile.rgroups', 'agents');
    }

    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']);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *