Use the below example code to use Transient API to store HTML String, or Object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function my_get_some_value($key) { // by default no cache when debug and if no external object_cache $defUse = ! (defined('WP_DEBUG') && WP_DEBUG) && wp_using_ext_object_cache(); // make the usage of cache filterable $useCache = apply_filters('my_use_cache', $defUse); // return cached value if any if ($useCache && ($cached = get_transient($key))) { return $cached; } // no cached value, make sure your code works with no cache $value = my_get_some_value_in_some_expensive_way(); // set cache, if allowed $useCache and set_transient($key, $value, HOUR_IN_SECONDS); return $value; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.