Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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!

like image 373
Ben Daggers Avatar asked Oct 18 '15 08:10

Ben Daggers


People also ask

What is cache fragmentation?

Fragment caching refers to caching a fragment of a Web page. For example, if a page displays a summary of yearly sale in a table, you can store this table in cache to eliminate the time needed to generate this table for each request. Fragment caching is built on top of data caching.

What is W3 Total Cache plugin?

W3 Total Cache (W3TC) improves the SEO, Core Web Vitals and overall user experience of your site by increasing website performance and reducing load times by leveraging features like content delivery network (CDN) integration and the latest best practices.


1 Answers

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.

like image 110
Mikel Bitson Avatar answered Oct 26 '22 22:10

Mikel Bitson