I am writing a very small cache plugin called Tiny Cache. It would cache only the_content() calls which take - I think - the longest time to generate during a page load.

I am able to hook the_content and store it in the object cache.

How can this plugin serve cached content? How to stop the_content filter from being processed?

remove_all_filters() would destroy all further the_content() calls. Maybe a way to stash filters away and restore them later?

Thank you.

1 Answer 1


The first implementation is in a new function called the_content_cached.

function the_content_cached( $more_link_text = null, $strip_teaser = false ) {

    $post_id = get_the_ID();
    // Not possible to tie content to post ID
    if ( ! $post_id ) {
        the_content( $more_link_text, $strip_teaser );

    $cached = wp_cache_get( $post_id, 'the_content' );
    // Cache miss
    if ( ! $cached ) {
        add_filter( 'the_content', 'tiny_cache_save_the_content', PHP_INT_MAX );
        the_content( $more_link_text, $strip_teaser );
        remove_filter( 'the_content', 'tiny_cache_save_the_content', PHP_INT_MAX );

    // Cache hit
    $timestamp = gmdate( 'c' );
    $message_tpl = '<!-- Cached content generated by Tiny cache on %s -->';
    print $cached;
    printf( $message_tpl, $timestamp );

// Save the content to the object cache
function tiny_cache_save_the_content( $content ) {

    $post_id = get_the_ID();
    // Tie content to post ID
    if ( $post_id ) {
        wp_cache_set( $post_id, $content, 'the_content' );

    return $content;
  • Is this function solving your issue? according to this answer, wp_cache_set() is a non-persistent cache. It can help if you need to request the same content multiple times during page load but won't persist the cached version. It will still process the same query on each load.
    – Key
    Mar 14, 2023 at 20:07
  • I may not mentioned that persistent object cache is in my default WordPress installation. github.com/szepeviktor/wordpress-website-lifecycle Mar 16, 2023 at 1:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.