This is an old revision of the document!


Let's take Caldera Forms Version 1.5.6.1 as a sample.

In

caldera-forms/classes/render/nonce.php

line 86

public static function nonce_field( $form_id )

is where the form nonce is generated. So to convert it to ESI, change <syntaxhighlight lang='php'>

public static function nonce_field( $form_id ){
	$nonce_field = '<input type="hidden" id="' . esc_attr( self::nonce_field_name( $form_id ) ) . '" name="' . esc_attr( self::nonce_field_name() ) . '" value="' . esc_attr( self::create_verify_nonce( $form_id ) ) . '"  data-nonce-time="' . esc_attr( time() ) . '" />';
	$nonce_field .= wp_referer_field( false );
	return $nonce_field;
}

</syntaxhighlight>

to

<syntaxhighlight lang='php'>

public static function nonce_field( $form_id, $from_esi = false ){
	if ( ! $from_esi ) {
		if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
			if ( method_exists( 'LiteSpeed_Cache_API', 'v' ) && LiteSpeed_Cache_API::v( '1.2.4' ) ) {
				$params = array( 'form_id' => $form_id ) ;
				return LiteSpeed_Cache_API::esi_url( 'caldera_forms', 'Caldera Forms', $params ) ;
			}
		}
	}
	$nonce_field = '<input type="hidden" id="' . esc_attr( self::nonce_field_name( $form_id ) ) . '" name="' . esc_attr( self::nonce_field_name() ) . '" value="' . esc_attr( self::create_verify_nonce( $form_id ) ) . '"  data-nonce-time="' . esc_attr( time() ) . '" />';
	$nonce_field .= wp_referer_field( false );
	return $nonce_field;
}
/**
 * Handle ESI request
 *
 */
public static function hook_esi( $params ) {
	$form_id = $params[ 'form_id' ] ;
	echo self::nonce_field( $form_id, true ) ;
	exit ;
}

</syntaxhighlight>

Then go to

caldera-forms/classes/core.php

, in

function __construct()

line 146 or other preferred position, add this: <syntaxhighlight lang='php'>

	if ( method_exists( 'LiteSpeed_Cache_API', 'esi_enabled' ) && LiteSpeed_Cache_API::esi_enabled() ) {
		LiteSpeed_Cache_API::hook_tpl_esi( 'caldera_forms', 'Caldera_Forms_Render_Nonce::hook_esi' );
	}

</syntaxhighlight>

Now Caldera Forms can be perfectly cached with ESI on.

  • Admin
  • Last modified: 2017/09/24 15:11
  • by Hai Zheng