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.

Read More

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

that means, the website that has been cached will be shown to the next visitor. example, visitor #1 accessed my site using his smart phone. it will show the mobile site correcrly. then the caching process will do its job. when another visitor (totally different person) visitor #2 has accessed my site through different device (let’s say) desktop. the site that will be shown will be the mobile desktop. I believe that’s because of the W3TC caching plugin. not sure im making sense here.

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!

Related posts

1 comment

  1. Based on the description of your problem this isn’t being caused by W3TC in general, but rather by the Page Cache functionality that W3TC provides. In the W3TC settings within WordPress, shut off page cache and only use the fragment cache and you will not have this problem.

    Or, load in this special content with javascript after the initial page load on mobile devices so that the page can be cached with W3TC without causing problems.

Comments are closed.