Last active
December 22, 2015 15:08
-
-
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/
This file contains hidden or 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 | |
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