Created
July 7, 2019 07:20
-
-
Save tricarte/7a002711bffd64d67a9ce319190e67c1 to your computer and use it in GitHub Desktop.
wordpress fragment cache
This file contains 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 | |
// 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