Skip to content

Instantly share code, notes, and snippets.

@webdev1001
Forked from westonruter/gist:5475349
Last active August 29, 2015 14:11
Show Gist options
  • Save webdev1001/613ae8b315a3188f234e to your computer and use it in GitHub Desktop.
Save webdev1001/613ae8b315a3188f234e to your computer and use it in GitHub Desktop.
<?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