Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
litespeed_wiki:cache:lscwp:esi_sample [2017/09/25 20:12]
Hai Zheng
litespeed_wiki:cache:lscwp:esi_sample [2018/04/25 21:48] (current)
Hai Zheng
Line 1: Line 1:
-Assume the following code in one WordPress plugin is some private info in one page.+Assume the following code in one WordPress plugin is some private info in one page:
  
-<​code>​+<​code ​php>
 function show( $id ) { function show( $id ) {
-    return "ID: $id <a href='​test?​nonce=$nonce'>​Click Me</​a>";​+    return "ID: $id <a href='​test?​nonce=" . wp_create_nonce() . "'>​Click Me</​a>";​
 } }
  
Line 10: Line 10:
  
 With LSCWP ESI API, it can be changed to this: With LSCWP ESI API, it can be changed to this:
-<​code>​+ 
 +==== Solution One ==== 
 + 
 +<​code ​php>
 function show( $id, $from_esi = false ) { function show( $id, $from_esi = false ) {
     // To make sure it is the original call     // To make sure it is the original call
Line 17: Line 20:
         if( method_exists( '​LiteSpeed_Cache_API',​ '​esi_enabled'​ ) && LiteSpeed_Cache_API::​esi_enabled() ) {         if( method_exists( '​LiteSpeed_Cache_API',​ '​esi_enabled'​ ) && LiteSpeed_Cache_API::​esi_enabled() ) {
             // To make sure is using the compatible API version             // To make sure is using the compatible API version
-            if ( method_exists( '​LiteSpeed_Cache_API',​ '​v'​ ) && LiteSpeed_Cache_API::​v( '1.2.4' ) ) {+            if ( method_exists( '​LiteSpeed_Cache_API',​ '​v'​ ) && LiteSpeed_Cache_API::​v( '1.3' ) ) {
          $params = array( '​id'​ => $id ) ;// If you have any parameters want to pass          $params = array( '​id'​ => $id ) ;// If you have any parameters want to pass
                 // Let's turn this block to ESI by returning directly                 // Let's turn this block to ESI by returning directly
Line 24: Line 27:
  }  }
     }     }
-    return "ID: $id <a href='​test?​nonce=$nonce'>​Click Me</​a>";​+    return "ID: $id <a href='​test?​nonce=" . wp_create_nonce() . "'>​Click Me</​a>";​
 } }
  
Line 31: Line 34:
  
 And then, in your main process, hook the ESI call to the real content: And then, in your main process, hook the ESI call to the real content:
-<​code>​+<​code ​php>
 if ( method_exists( '​LiteSpeed_Cache_API',​ '​esi_enabled'​ ) && LiteSpeed_Cache_API::​esi_enabled() ) { if ( method_exists( '​LiteSpeed_Cache_API',​ '​esi_enabled'​ ) && LiteSpeed_Cache_API::​esi_enabled() ) {
     LiteSpeed_Cache_API::​hook_tpl_esi('​my_plugin_esi',​ '​hook_esi'​ );     LiteSpeed_Cache_API::​hook_tpl_esi('​my_plugin_esi',​ '​hook_esi'​ );
Line 42: Line 45:
 } }
 </​code>​ </​code>​
 +
 +
 +
 +
 +==== Solution Two ====
 +
 +Assuming ESI is ON and LSCWP is up-to-date
 +
 +Separated `show()` function to make it easier to understand:
 +<code php>
 +
 +LiteSpeed_Cache_API::​hook_tpl_esi('​my_plugin_esi',​ '​hook_esi'​ );
 +
 +function hook_esi( $param ) {
 +    $id = $params[ '​id'​ ] ;
 +    echo "ID: $id <a href='​test?​nonce="​ . wp_create_nonce() . "'>​Click Me</​a>";​
 +    exit;
 +}
 +
 +function show( $id ) {
 +    $params = array( '​id'​ => $id ) ;
 +    return LiteSpeed_Cache_API::​esi_url( '​my_plugin_esi',​ 'My Plugin Name', $params ) ;
 +}
 +
 +echo show(35);
 +
 +</​code>​
 +
  
 ===== More Sample Codes ===== ===== More Sample Codes =====
  • Admin
  • Last modified: 2017/09/25 20:12
  • by Hai Zheng