Title: Create custom ESI block without using shortcode
Last modified: February 12, 2019

---

# Create custom ESI block without using shortcode

 *  Resolved [arabtornado](https://wordpress.org/support/users/arabtornado/)
 * (@arabtornado)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/)
 * 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](https://wordpress.org/support/users/arabtornado/).

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

 *  Plugin Support [Hai Zheng⚡](https://wordpress.org/support/users/hailite/)
 * (@hailite)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11198477)
 * 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](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⚡](https://wordpress.org/support/users/hailite/).
 *  Thread Starter [arabtornado](https://wordpress.org/support/users/arabtornado/)
 * (@arabtornado)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11198801)
 * 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](https://wordpress.org/support/users/arabtornado/).
 *  Plugin Support [LiteSpeed Lisa](https://wordpress.org/support/users/lclarke/)
 * (@lclarke)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11198942)
 * 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](https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:lscwp: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](https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:developer_guide:cache-vary).
 *  Thread Starter [arabtornado](https://wordpress.org/support/users/arabtornado/)
 * (@arabtornado)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11199206)
 * 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⚡](https://wordpress.org/support/users/hailite/)
 * (@hailite)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11201440)
 * This ESI example may help you understand: [https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:lscwp:esi_sample](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](https://wordpress.org/support/users/arabtornado/)
 * (@arabtornado)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11201985)
 * Thank you, I tried the first “Time” example but as you can see here
    [https://bonstato.net](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⚡](https://wordpress.org/support/users/hailite/)
 * (@hailite)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11202318)
 * 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⚡](https://wordpress.org/support/users/hailite/)
 * (@hailite)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11202323)
 * 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](https://wordpress.org/support/users/arabtornado/)
 * (@arabtornado)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11203662)
 * 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.

 * ![](https://ps.w.org/litespeed-cache/assets/icon-256x256.png?rev=2554181)
 * [LiteSpeed Cache](https://wordpress.org/plugins/litespeed-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/litespeed-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/litespeed-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/litespeed-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/litespeed-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/litespeed-cache/reviews/)

 * 9 replies
 * 3 participants
 * Last reply from: [arabtornado](https://wordpress.org/support/users/arabtornado/)
 * Last activity: [7 years, 4 months ago](https://wordpress.org/support/topic/create-custom-esi-block-without-using-shortcode/#post-11203662)
 * Status: resolved