• Resolved CrazySerb

    (@crazyserb)


    We are trying to exclude a part of the PHP template / certain section of code from being cached, as it sets a bunch of parameters and cookies for users, so is there any way to add some starting/ending tags around that code to have the LS Cache ignore it when caching it, like other caching solutions have?

    • This topic was modified 1 year, 6 months ago by CrazySerb.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support qtwrk

    (@qtwrk)

    I wonder how other caching solution do it , but in our case, we have ESI,

    please check this page , if it’s shortcode , you can use ESI shortcode , like [your_short_code] to [esi your_short_code ttl="0"]

    Thread Starter CrazySerb

    (@crazyserb)

    @qtwrk yeah, it’s not a shortcode, it’s just straight up PHP code section or two, some having include_once() and require_once() PHP file includes (files that we would like to avoid caching, but I can’t add them in LS Cache configuration as URIs).

    So any other suggestions, as that page you referenced seems to work only with functions.php file and shortcodes/blocks/widgets. We can’t just convert this code of ours to shortcode/block/widget as it’s unique to each template we have it on and it’s not feasible to do so.

    Plugin Support qtwrk

    (@qtwrk)

    no , the example was quick and easy , to explain in functions.php , but it can be used elsewhere in your theme or plugins.

    Thread Starter CrazySerb

    (@crazyserb)

    @qtwrk, ok so how would I use it otherwise then, around a block of PHP code in a PHP template?

    I still don’t get it.

    Can you give me an example of that, without it referring to any shortcodes, functions.php or widgets?

    Thanks!

    Plugin Support qtwrk

    (@qtwrk)

    not sure what kind of template you refer to , but I got another example I made long time ago , see if it helps you

    <?php
    /*
    Plugin Name: LiteSpeed Cache Plugin ESI
    Description: LiteSpeed ESI test
    */
    
    defined('WPINC') || exit;
    
    if (!defined('LSCWP_V') || ! apply_filters( 'litespeed_esi_status', false ))
    {
        return;
    }
    
    add_action( 'litespeed_esi_load-my_esi_block', 'my_esi_block_esi_load' );
    
    function my_esi_block_esi_load()
    {
    do_action( 'litespeed_control_set_nocache' );
    echo "Hello world: ".rand (1,99999);
    }
    
    add_action('wp_head', 'lscwp_esi_test'); 
    add_action('wp_footer', 'lscwp_esi_test'); 
    function lscwp_esi_test() { 
    	echo '<div style="background: blue; color: white; text-align: center;">';
        echo apply_filters( 'litespeed_esi_url', 'my_esi_block', 'Custom ESI block' );
    	echo '</div>';
    }
    

    this works as a plugin , and add a random number in header and footer as example case.

    basically , for whereever you want to display content , you echo out the apply_filters( 'litespeed_esi_url', 'my_esi_block', 'Custom ESI block' )

    and then create a corresponding function, somewhere is called before apply_filters , in above example , I called ESI function like few lines before as demonstration.

    Thread Starter CrazySerb

    (@crazyserb)

    Again, that does nothing to demonstrate how it’s possible to exclude a certain php code from being cached in a regular php template.

    All I have been seeing so far from you and from my search results is these references to apply_filters, functions.php or custom plugin through which I have to create that specific block of text/code that can then somehow be excluded.

    I am not in a position to do that, and just need to exclude a block of variables and one require_once() line from a php template like this:

    <?php
    ... some PHP code that needs to be cached...

    $var1 = "...";
    $var2 = "...";
    require_once("/home/file.php");

    ...some more PHP code that needs to be cached...
    ?>

    And I just need the code in the middle excluded from caching, including that /home/file.php and all the code inside it, as that’s where all sorts of dynamic variables are set for each unique user.

    HOW do I accomplish that with Litespeed Cache without having to create additional plugins, re-declare all this code as some sort of a custom widget, block or a plugin?

    Thread Starter CrazySerb

    (@crazyserb)

    I have tried using this approach as well:

    https://docs.litespeedtech.com/lscache/lscwp/api/#generate-esi-block-url

    but I can’t just print out a content from another, external PHP file in function my_esi_block_esi_load() by using require_once(“/home/domain/file.php”); as that does nothing (doesn’t print out the contents of that file at all).

    So that round-about solution still doesn’t work for what I’m dealing with here, which is exclude a whole .php file and its contents (that gets inserted into certain templates) from being cached, along with certain variables I set dynamically before including that file, as they are unique to each visitor.

    Plugin Support qtwrk

    (@qtwrk)

    /wp-content/plugins/lscwp-esi/fake_require.php:

    <?php 
    echo "Hello world from required file: ".rand (1,99999);
    echo '<br>';
    echo 'I received variables: <br>';
    echo var_dump($params);

    /wp-content/plugins/lscwp-esi/lscwp-esi.php :

    <?php
    /*
    Plugin Name: LiteSpeed Cache Plugin ESI
    Description: LiteSpeed ESI test
    */
    
    defined('WPINC') || exit;
    
    if (!defined('LSCWP_V') || ! apply_filters( 'litespeed_esi_status', false ))
    {
        return;
    }
    
    add_action( 'litespeed_esi_load-my_esi_block', 'my_esi_block_esi_load' );
    
    function my_esi_block_esi_load($params)
    {
    do_action( 'litespeed_control_set_nocache' );
    require_once( WP_CONTENT_DIR . "/plugins/lscwp-esi/fake_require.php");
    }
    
    add_action('wp_head', 'lscwp_esi_test'); 
    add_action('wp_footer', 'lscwp_esi_test'); 
    function lscwp_esi_test() {
    		$var1 = '123';
    		$var2 = 'abc';
    		echo '<div style="background: blue; color: white; text-align: center;">';
        	echo apply_filters( 'litespeed_esi_url', 'my_esi_block', 'Custom ESI block', array($var1,$var2) );
    		echo '</div>';
    }

    you will see output like this blue background content in head and footer where the number will constantly change upon each refresh while main page is cached

    for $var1 and $var2 , it is better to get or set them inside of ESI block , otherwise it might be fixed value on first visit.

    Thread Starter CrazySerb

    (@crazyserb)

    Hmm, that did not work for me at all – I can’t even get anything to print out from that function lscwp_esi_test() itself, before apply_filters echo statement…

    Is there anything else in LS Cache plugin that I need to turn ON or OFF to make this work, other than turning ESI to ON that I might be missing somehow?

    Plugin Support qtwrk

    (@qtwrk)

    no , only need to enable ESI, and make sure you are using LiteSpeed Enterprise, LiteSpeed ADC or QUIC cloud CDN, the OpenLiteSpeed does NOT support ESI

    did you try my demo plugin above ? does that work ?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to exclude part of a PHP template / page from being cached?’ is closed to new replies.