-
-
Save mike-at-redspace/1be0ac96b54ed4e07de18c4f369c8115 to your computer and use it in GitHub Desktop.
WordPress Fragment Caching convenience wrapper
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 | |
/* | |
Usage: | |
cache_fragment_output( 'unique-key', 3600, function () { | |
functions_that_do_stuff_live(); | |
these_should_echo(); | |
}); | |
*/ | |
function cache_fragment_output( $key, $ttl, $function ) { | |
$group = 'fragment-cache'; | |
$output = wp_cache_get( $key, $group ); | |
if ( empty($output) ) { | |
ob_start(); | |
call_user_func( $function ); | |
$output = ob_get_clean(); | |
wp_cache_add( $key, $output, $group, $ttl ); | |
} | |
echo $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment