• Resolved John Webber

    (@bilion)


    Hello, Hummingbird 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 Hummingbird 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 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @bilion,

    I hope you are doing well today!

    We are checking your request with our developers if it is possible to provide a workaround or custom code to achieve this and will inform you accordingly.

    Thanks for the patience while we are looking into this.

    Kind regards,
    Zafer

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @bilion,

    Our developers informed that, as a start point, you can use the wphb_should_cache_request_pre filter to determine whether a request must be cached or not.

    Example code:

    <?php
    
    //
    // See: wp-content/plugins/hummingbird-performance/core/modules/class-page-cache.php:1204
    //
    
    add_filter( 'wphb_should_cache_request_pre', function( $cache ) {
    	// Custom logic here:
    	$should_cache = false;
    
    	if ( ! $should_cache ) {
    		$cache = false; // <-- Do not cache this request.
    	}
    
    	return $cache;
    });

    Please also note that since it is a custom development, our scope of support that we can provide here is limited to above only. Thanks for your understanding.

    Kind regards,
    Zafer

    Thread Starter John Webber

    (@bilion)

    Hello,?@wpmudevsupport15

    I found a solution, but did not test with your plugin

    Defining the DONOTCACHEPAGE constant for a specific page works for most caching plugins. Maybe it works for yours as well

    And if it doesn’t, you should update it to respect this standard

    Thanks,
    Jovan

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @bilion

    Yes, the DONOTCACHEPAGE is supported by Hummingbird so if it’s detected such page is not cached.

    You can easily confirm that by enabling the debug log of cache (“Enable debug log” option on Page Cache settings) and then checking the log after few visits to such pages. You’ll see entries in log that page (though it will not state which page actually!) was not cached due to DONOTCACHEPAGE present.

    Note though: we didn’t suggest it because you specifically asked about preventing caching on first visit rather than preventing caching entirely. The DONOTCACHEPAGE stops caching of a given page entirely – not just for the first visit.

    Best regards,
    Adam

    Thread Starter John Webber

    (@bilion)

    Hi @wpmudev-support

    You are right. My initial code and question were not in fact what I needed.
    Thank you for your help. This is the solution I require.

    Best regards,
    Jovan

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