• Resolved John Webber

    (@bilion)


    Hello, WP Super Cache Support Team,

    I’m developing a performance improving plugin and have a specific caching requirement.

    I need pages and posts not to be cached on first open, after they are created or edited.

    I need to examine the page with all the JS and CSS implemented, then edit the HTML before it can be cached. So, the page should be cached on the second visit.

    Is there a built-in feature or specific hook in WP Super Cache that supports this functionality, and can be controlled through PHP?

    I am considering a custom implementation using WordPress hooks. Here’s a brief overview of my approach:

    // Function to update a custom field when a post or page is saved
    
    function update_last_modified_meta($post_id) {
    
        update_post_meta($post_id, '_last_modified_time', current_time('mysql'));
    
    }
    
    add_action('save_post', 'update_last_modified_meta');
    
    // Function to control caching based on the last modified time
    
    function custom_per_page_cache_control() {
    
        if (is_single() || is_page()) {
    
            global $post;
    
            $stored_last_modified = get_post_meta($post->ID, '_last_modified_time', true);
    
            if ($post->post_modified !== $stored_last_modified) {
    
                // Send headers to prevent caching
    
                header("Cache-Control: no-cache, must-revalidate, max-age=0");
    
                header("Pragma: no-cache");
    
                header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
    
                update_post_meta($post->ID, '_last_modified_time', $post->post_modified);
    
            }
    
        }
    
    }
    
    add_action('template_redirect', 'custom_per_page_cache_control');

    Is this approach is compatible with your plugin? Is there a solution specific to your plugin?

    Any recommendations or best practices for implementing this would be greatly appreciated.

    Thank you,

    Jovan

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Donncha O Caoimh (a11n)

    (@donncha)

    You can modify or look at the output buffer before it’s saved to a file using the “wp_cache_ob_callback_filter” filter. That takes one argument, the buffer, which is the HTML of the entire page.

    You can modify the HTML, and then it will be saved and sent to the browser.

    Plugin Support Tamirat B. (a11n)

    (@tamirat22)

    It’s been one week since this topic was last updated. I’m going to mark this thread as solved. If you have any further questions or need more help, you’re welcome to open another thread here. Cheers!

    Thread Starter John Webber

    (@bilion)

    Hi @donncha, @tamirat22

    I guess I missed the email about your response.

    My initial question was not what I actually need. I’m lookin to not cache only the page we are on, on first open. I did find a solution that works with most plugins.

    Tested it with your plugin. Works well.

    Thank you!

    add_action('send_headers', 'combined_cache_control');
    function combined_cache_control() {
        global $post;
    
        // Check if we are on a post page and the post has a cache prevention flag
        if (is_a($post, 'WP_Post')) {
            $prevent_cache = get_post_meta($post->ID, '_prevent_cache', true);
    
            if ($prevent_cache) {
                // Send headers to prevent caching
                header("Cache-Control: no-cache, must-revalidate, max-age=0");
                header("Pragma: no-cache");
                header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
    
                // Also prevent caching using WordPress constants if available
                if (!defined('DONOTCACHEPAGE')) {
                    define('DONOTCACHEPAGE', true);
                }
            }
        }
    }
    
    
    add_action('save_post', 'set_prevent_cache_flag');
    function set_prevent_cache_flag($post_id) {
        // Set the flag when a post is saved
        update_post_meta($post_id, '_prevent_cache', '1');
    }
    
    • This reply was modified 12 months ago by John Webber.
    Plugin Author Donncha O Caoimh (a11n)

    (@donncha)

    Ah, that’s a neat way of doing it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Prevent Caching On First Open’ is closed to new replies.