Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created January 12, 2011 20:51
Show Gist options
  • Save westonruter/776855 to your computer and use it in GitHub Desktop.
Save westonruter/776855 to your computer and use it in GitHub Desktop.
WordPress [esi] Shortcode for doing Edge Side Includes (ESI)
<?php
/**
* Edge Side Includes (ESI) Shortcode
*/
function esi_shortcode($attrs){
$attrs = shortcode_atts(array(
'src' => '',
'ttl' => '1h',
'dca' => 'none',
'onerror' => 'continue'
), $attrs);
// Serialize $attrs into HTML attributes
$html_attrs = array();
foreach($attrs as $name => $value){
$html_attrs[] = $name . '="' . esc_attr($value) . '"';
}
// Yes, we return the esi shortcode back with the updated attributes because
// WordPress will strip out <esi:include> at this point. The content filter
// below will do the final replacement just in time.
return sprintf('[esi %s ]', join(' ', $html_attrs));
}
add_shortcode( 'esi', 'esi_shortcode' );
/**
* Since <esi:include> tags don't make it through the WordPress content filter
* when output by shortcode, do the replacement at the end.
*/
function esi_shortcode_content_filter( $content ){
return preg_replace_callback( '/\[esi(.+?)\]/', 'esi_shortcode_content_filter_callback', $content );
}
add_filter( 'the_content', 'esi_shortcode_content_filter', 100 );
/**
* This is because Akamai b0rks on &amp; appearing in esi:include attributes
*/
function esi_shortcode_content_filter_callback( $matches ){
$attrs = str_replace( '&amp;', '&', $matches[1] );
return "<esi:include $attrs />";
}
@Nyholm
Copy link

Nyholm commented Aug 4, 2015

Just for clarification:

[esi src="http://example.com ttl="3h"]

will produce...

<esi:include src="http://example.com ttl="3h" dca="none" onerror="continue" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment