[Hummingbird Pro] Clear cache on interval only for specific pages

3

Currently we can set hummingbird to clear cache on interval, set to let’s say 24 hours. This clears the entire site cache. I would like to specify which pages I would like cleared on interval – not the whole website.

I have a use case where a specific page on my website is generated dynamically; think of a portfolio page with randomly ordered content. Unfortunately, caching this page means the content is no longer populated dynamically in a random order – that’s kind of the point of caching – instead the cached copy of the page shows the content in the same order every time it is loaded.

Thankfully Hummingbird allows me to set the clear cache on a specific interval. After an automated purge, this page will be now be re-cached with a new set of dynamically ordered content.

The problem with this is I don’t want to clear the cache on my entire site, just the one page. What I am requesting is an expansion of the Clear cache on interval functionality of Hummingbird to support clearing specific pages on interval.

I could disable caching entirely on that page, but that means 100% of visits to the page will be dynamically generated – causing more server strain. What if this page was my homepage, or my most popular landing page? I would want to cache it, but say purge it every 24 hours to display new content.

What are your thoughts on this?

  • Patrick Freitas
    • FLS

    Hi Phil

    I hope you are doing well.

    You can use a custom script for this, could you please try:

    <?php
    
    if ( ! wp_next_scheduled( 'cron_hook_clear_caches' ) ) {
        wp_schedule_event( time(), 'daily', 'cron_hook_clear_caches' );
    }
    
     add_action( 'cron_hook_clear_caches', 'cron_hook_clear_caches_function' );
    
    function cron_hook_clear_caches_function() {
    	$pages = [ 1, 2, 3, 658 ];
    
    	foreach( $pages as $page ){
    		do_action( 'wphb_clear_page_cache', $page );
    	}
    }

    This add a daily cron for clear the cache on pages that it is included in the array, I wouldn’t suggest making it to a massive number of pages but could be helpful for some pages.

    We are also adding this feature request to our developers.

    Best Regards
    Patrick Freitas

  • Shweta
    • New Recruit

    I’m trying the same thing – and I don’t think it’s triggering the cache for the homepage with it’s ID. The homepage is set to be pre-loaded, so would that make it a special case? The action is not being called.

    // Replace ‘your_custom_post_type’ with your actual custom post type
    add_action(‘publish_post’, ‘uncso_clear_cache_for_specific_page’, 10, 2);

    function uncso_clear_cache_for_specific_page($post_id, $post) {

    echo “<h2> HERE </h2>”;
    //Homepage ID in dev and prod
    $pages = [ 32 ];

    echo “Clearing page cache for ” . print_r($pages , 1);

    foreach( $pages as $page ){
    do_action( ‘wphb_clear_page_cache’, $page );

    }

    exit();

    }

  • Adam
    • Support Gorilla

    Hi Shweta

    It should work but:

    1. do not use “echo” function inside the callback function; instead if you want to track if the function was executed, better use error_log to write to log file (you’ll need WP_DEBUG and WP_DEBUG_LOG enabled) – otherwise it will break post/page saving.

    2. do not use “exist()” as it will break execution of anything further after the code.

    This should work fine:

    add_action('publish_post', 'uncso_clear_cache_for_specific_page', 10, 2);
    
    function uncso_clear_cache_for_specific_page($post_id, $post) {
    
    	error_log( "HERE" );
    	//Homepage ID in dev and prod
    	$pages = [ 32 ];
    
    	error_log( "Clearing page cache for " . print_r($pages , 1) );
    
    	foreach( $pages as $page ){
    		do_action( 'wphb_clear_page_cache', $page );
    	}
    
    }

    Best regards,
    Adam