Skip to content

Instantly share code, notes, and snippets.

@tricarte
Created July 7, 2019 07:20
Show Gist options
  • Save tricarte/7a002711bffd64d67a9ce319190e67c1 to your computer and use it in GitHub Desktop.
Save tricarte/7a002711bffd64d67a9ce319190e67c1 to your computer and use it in GitHub Desktop.
wordpress fragment cache
<?php
// https://css-tricks.com/wordpress-fragment-caching-revisited/
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