Skip to content

Instantly share code, notes, and snippets.

@danalmeida
Last active December 22, 2015 15:08
Show Gist options
  • Save danalmeida/6490127 to your computer and use it in GitHub Desktop.
Save danalmeida/6490127 to your computer and use it in GitHub Desktop.
Fragment caching takes the output of a code block and stores it so for a predetermined amount of time. http://css-tricks.com/wordpress-fragment-caching-revisited/
<?php
function fragment_cache($key, $ttl, $function) {
if ( is_user_logged_in() ) {
call_user_func($function);
return;
}
$key = apply_filters('fragment_cache_prefix','fragment_cache_').$key;
$output = get_transient($key);
if ( empty($output) ) {
ob_start();
call_user_func($function);
$output = ob_get_clean();
set_transient($key, $output, $ttl);
}
echo $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment