Created
January 12, 2011 20:51
-
-
Save westonruter/776855 to your computer and use it in GitHub Desktop.
WordPress [esi] Shortcode for doing Edge Side Includes (ESI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 & appearing in esi:include attributes | |
*/ | |
function esi_shortcode_content_filter_callback( $matches ){ | |
$attrs = str_replace( '&', '&', $matches[1] ); | |
return "<esi:include $attrs />"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just for clarification:
will produce...