• Resolved arabtornado

    (@arabtornado)


    Hi,

    Im wondering if i have a specific function that i added inside functions.php and attaching it to a hook, Can i exclude its output from page caching without the need to create a shortcode for it maybe using a specific Litespeed hook/filter that i’m not aware off?

    Thanks

    • This topic was modified 7 years, 4 months ago by arabtornado.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Hai Zheng⚡

    (@hailite)

    Did you mean to keep that output no-cacheable? If so, the whole page will be non cacheable in fact. You can call our ESI API without using shortcode.

    https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:lscwp:api

    LiteSpeed_Cache_API::hook_tpl_not_esi($hook)
    Hooki not ESI template.
    
    LiteSpeed_Cache_API::hook_tpl_esi($block, $hook)
    Use this hook to display an ESI block.
    • This reply was modified 7 years, 4 months ago by Hai Zheng⚡.
    Thread Starter arabtornado

    (@arabtornado)

    Thanks for your reply and sorry that my question wasn’t clear enough, I will try to clearify what i mean more.

    This is an example code that change the Woocommerce price based on the user country using Cloudflare GeoIP HTTP header.

    So i was wondering if i can make the outbut of this function (The price) uncachable while the rest of the page to be cashed, Please note that i used on this example add_filter and add_action to make sure of the posability to do so in both cases.

    function sv_change_product_price_display( $price ) {
    	$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];
    	if ( $country_code = "DE") {
    		$price = $price + 4;	
    	} else {
    		$price = $price + 2;	
    	}
    	return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );
    add_action( 'woocommerce_before_add_to_cart_button', 'sv_change_product_price_display' );
    • This reply was modified 7 years, 4 months ago by arabtornado.
    Plugin Support LiteSpeed Lisa

    (@lclarke)

    There may be an easier way to do this.

    You can create cache varies based on country. So, there would be a fully cached version of each page for users from DE and a separate fully cached version for everyone else.

    Check our API for relevant “vary” functions. I think this is the one you’d need:
    LiteSpeed_Cache_API::vary($vary_name, $vary_value, $default_value)

    You can learn more about cache varies here.

    Thread Starter arabtornado

    (@arabtornado)

    Thanks again for the quick response 🙂
    I read the documentation but still having a hard time to understand it… maybe because im still new to programing and the lack of examples on the documentation.

    Anyway here is what i still dont understand hope you can clear it up for me..

    ——————————
    1- To make it very simple lets use this example to output the time into the wp_head hook using ESI

    Here is the non-ESI code

    add_action( 'wp_head', 'esi_time' );
    function esi_time() {
        echo date("h:i:sa");
    }

    So im wondering how to implement the sample code above with the following ESI function? LiteSpeed_Cache_API::hook_tpl_esi($block, $hook)

    ————————————-
    2- I read the documentation and what i understood is that i have to add vary value to the http header in htaccess using code like this
    RewriteCond %{HTTP_USER_AGENT} Mobile|Android|Silk/|Kindle|BlackBerry|Opera\ Mini|Opera\ Mobi [NC] RewriteRule .* – [E=Cache-Control:vary=ismobile]

    So please correct me if im wong, my guess for how to use LiteSpeed_Cache_API::vary() function is like that..

    add_action( 'wp_head', 'esi_device' );
    function esi_device() {
        LiteSpeed_Cache_API::vary('ismobile', 'This is mobile' , 'This is a PC')
    }

    Also im wondering what if i needed to create more than 1 variation. for exammple if i have 5 countries and i wanna do a different variation for each country.

    Im sorry for the too many questions im just trying to understand the LS functions.

    Thanks

    Plugin Support Hai Zheng⚡

    (@hailite)

    This ESI example may help you understand: https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:lscwp:esi_sample

    1.

    
    add_action( 'wp_head', 'esi_time' );
    function esi_time() {
        echo LiteSpeed_Cache_API::esi_url( 'show_your_time', 'xxx Name' ) ;
    }
    
    LiteSpeed_Cache_API::hook_tpl_esi('show_your_time', 'show_your_time' ) ;
    function show_your_time( $param ) {
        echo date("h:i:sa") ;
    }
    
    

    2. If you want to use vary way, for different countries, you can just use one vary, with different values.
    So it will be like

    
    LiteSpeed_Cache_API::vary('geo', 'US' , 'US') ;
    
    LiteSpeed_Cache_API::vary('geo', 'CH' , 'US') ;
    
    Thread Starter arabtornado

    (@arabtornado)

    Thank you, I tried the first “Time” example but as you can see here
    https://bonstato.net
    the time is cached however the ESI TTL on the plugin settings is set to 0, also please note that i see a different time when i login as administrator or not however all user roles on ESI tab has 0 TTL

    Plugin Support Hai Zheng⚡

    (@hailite)

    That TTL is in the settings. As you called API, you can set TTL by yourself (sample below). BTW, the roles settings under ESI tab is not a TTL config.

    
    LiteSpeed_Cache_API::hook_tpl_esi('show_your_time', 'show_your_time' ) ;
    function show_your_time( $param ) {
        LiteSpeed_Cache_API::set_ttl( 60 ) ;
        echo date("h:i:sa") ;
    }
    

    Also, when calling esi_url, would suggest to add a 4th param to control cache to public. Default is private which will cause each individual visitor hit your WP and lose the benefit of cache. Can call like this:

    echo LiteSpeed_Cache_API::esi_url( 'show_your_time', 'xxx Name', array(), 'public' ) ;

    Plugin Support Hai Zheng⚡

    (@hailite)

    However, this is just to show how to use ESI url. If the reason is to show different public info based on GEO location, vary is a better way like Lisa mentioned.

    Thread Starter arabtornado

    (@arabtornado)

    Thanks so much for your reply

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Create custom ESI block without using shortcode’ is closed to new replies.