How to implement Fragment Caching (W3TC) in WordPress

So I have this codes in my index.php file in my wordpress template:

if($detect->isMobile() && !$detect->isTablet() ) {
    include('mobileshares.php');
}

The codes’ purpose is to load a php file whenever the user is using a mobile or tablet device. That means, it will not be loaded if it’s desktop.

Anyway, it really works perfectly. NOT UNTIL I installed the W3 Total Cache plugin in my wordpress. The caching messes everything up.

So I have done some few readings and found an answer which is Fragment Caching.

After editing and adding few lines in my wp config file: here is my revised code now:

<!-- mfunc W3TC_DYNAMIC_SECURITY -->
    <?php

        if($detect->isMobile() && !$detect->isTablet() ) {
            include('mobileshares.php');
        }
    ?>
<!-- /mfunc W3TC_DYNAMIC_SECURITY -->

(This code goes to wp-config.php file)

define('W3TC_DYNAMIC_SECURITY', 'somesecurestring');

I have set my setting to “Disk: Basic” and enable the “Late initialization”, the code is still messed up!

What am I doing wrong? Could you please point out what’s wrong? Can you suggest a better way to implement this?

I appreciate all your answers/comments so I can settle this problem. Thanks!

Leave a comment