• Resolved Rhapsody348

    (@rhapsody348)


    I have multiple sites that I use the W3 Total Cache plugin. Some use the free version and the others the paid version. I would like to know the best approach to solve a problem when the home page is cached. The issue is that page caching does not accommodate some content differences for logged in users, and the WordPress menu structure changes when a user is logged in with unique links for logged in users such as “Edit my Profile”. Some sidebar widgets are populated with different content when a user is logged in.

    I have a custom plugin that provides similar functionality for the menu and sidebar widgets on these sites. Since I have total control on the widget content and menu, all served with data generated by PHP code, what is the best way using W3TC to address the issue?

    Should I use fragment caching? If so can the mfunc tags be inserted in the HTML output generated by the PHP code (e.g. before the start and after the end of the menu)?

    Should I generate AJAX calls to dynamically update certain areas of the page?

    Are there other approaches?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @rhapsody348

    The general rule is that you should not cache the pages for logged-in users, simply because of the reason you mentioned. Once cached, the pages will show for the next visitor as cached. This may lead to the sharing of some sensitive information.
    You cannot have separate cached pages from logged-in users and non-logged-in users. And even if that is available, you should have a separate cache based on the user to avoid sharing some sensitive/account details. This means, more caching, and more server resource consumption. Not to mention that the separate user caching would have to be based on the data that the user provides, and we cannot collect this data due to the GDPR rules.

    However, W3 Total Cache is an advanced plugin for a reason, and you can achieve what you need, but this means some specific configuration.

    As a guideline, you should disable caching for logged-in users and only allow caching for subscribers/customers (If this is the user role). Next, Browser Caching is a big NO in this case, so you should modify the Browser Cache for the HTML and set the expires headers to not cache and no-store. This will also impact non-logged-in users.

    And of course, any pages with sensitive information like checkout, cart, or account should be excluded from the cache.
    The login/logout is also something that should remain dynamic, so this depends on the plugin you are using for this. and you can use?Page Fragment Caching, and wrap the PHP code for the login/logout with the mfunc to exclude that part of the page from being cached. The <mfunc > should replace PHP tags you wish to remain dynamic. And those thags should not be within the PHP tag

    The best and safest way to approach this is to generate AJAX calls to update certain areas of the page dynamically. This will bypass the cache on specific areas of the page used with AJAX calls.

    I hope I explained this and let me know if you have any other questions.

    Thanks!

    Thread Starter Rhapsody348

    (@rhapsody348)

    Marko – this is a perfect explanation. Thanks for taking the time to put these details in. I will mark this topic as resolved.

    Thread Starter Rhapsody348

    (@rhapsody348)

    Marko – rereading this, I have a question regarding the comment:

    The <mfunc > should replace PHP tags you wish to remain dynamic. And those thags should not be within the PHP tag

    If php is creating the page content as part of a shortcode similar to the example below, may the <mfunc> tags be included? The Shortcode content is output with do_shortcode(‘[myshortcode]’). :

    <?php 
    other code here for the shortcode
    
    $content = "This has some sample generated data.";
    
    $content .= "The content may have " . $myvariable . " information in it.";
    
    $content .= "<mfunc>This should not be cached " . $another_variable . "</mfunc>.  The data in another_variable changes and should not be cached.";
    
    //Returns the shortcode generated content
    return $content;
    ?>

    If the <mfunc> tags may not be included in the PHP tags, how may the dynamic content in the shortcode use the Page Fragment Cache?

    • This reply was modified 11 months, 3 weeks ago by Rhapsody348.
    Plugin Contributor Marko Vasiljevic

    (@vmarko)

    Hello @rhapsody348

    You can try by closing php tag, adding mfunc and re-opening the php tag after the mfunc is closed.

    I hope this helps!

    Thanks!

    Thread Starter Rhapsody348

    (@rhapsody348)

    Thanks Marko!

    Confirming an approach with the <mfunc> tags outside the php tags. What I’m wondering is if the mfunc tags will appear in the proper place as I believe is they might be in the html that is output on the screen before the contents of the widget is rendered. The modified approach is shown below.:

    <?php 
    
    //other code here for the shortcode 
    
    $content = "This has some sample generated data."; 
    
    $content .= "The content may have " . $myvariable . " information in it.";
    
    //turn off the php tag for the mfunc tag
    ?><!-- <mfunc> this starts a fragment cache area -->
    <?php //the php tag is turned on after the mfunc tag
    
    $content .= "This should not be cached " . $another_variable . ".";
    
    //turn off the php tag for the mfunc tag
    >?
    <!-- </mfunc> this ends a fragment cache area -->
    <?php //the php tag is turned on after the mfunc tag
    
    $content .= " The data in another_variable changes and should not be cached."; 
    //Returns the shortcode generated content return $content; 
    ?>'
    • This reply was modified 11 months, 3 weeks ago by Rhapsody348.
    • This reply was modified 11 months, 3 weeks ago by Rhapsody348.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Suggested Approach to resolve Page Cache Dilemma’ is closed to new replies.