Assume the following code in one WordPress plugin is some private info in one page:

function show( $id ) {
    return "ID: $id <a href='test?nonce=" . wp_create_nonce() . "'>Click Me</a>";
}
 
echo show(35);

With LSCWP ESI API, it can be changed to this:

Solution One

function show( $id, $from_esi = false ) {
    // To make sure it is the original call
    if ( ! $from_esi ) {
        // To make sure LSCWP ESI is on
        if( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
            // To make sure is using the compatible API version
            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
                // Let's turn this block to ESI by returning directly
	        return LiteSpeed_Cache_API::esi_url( 'my_plugin_esi', 'My Plugin Name', $params ) ;
            }
	}
    }
    return "ID: $id <a href='test?nonce=" . wp_create_nonce() . "'>Click Me</a>";
}
 
echo show(35);

And then, in your main process, hook the ESI call to the real content:

if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
    LiteSpeed_Cache_API::hook_tpl_esi('my_plugin_esi', 'hook_esi' );
}
 
function hook_esi( $params ) {
    $id = $params[ 'id' ] ;
    echo show( $id, true ) ;
    exit;
}

Solution Two

Assuming ESI is ON and LSCWP is up-to-date

Separated `show()` function to make it easier to understand:

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);
  • Admin
  • Last modified: 2018/04/25 21:48
  • by Hai Zheng