Empty Super Cache programmatically (with ACF action)

I regularly use Advanced Custom Fields to create awesome backend interfaces for my clients (as I’m sure a lot of us do…). ACF includes an Options add-on that creates one or multiple global options pages that one can pull data from anywhere. I use the Options page to, for example, let the client select the 5 posts to be featured on a homepage carousel.

I have started running into issues using the Options add-on and Super Cache. It seems that by default saving the Options page has no effect on the cache and so for example the homepage carousel remains unchanged and the client is confused…

Read More

I need to hook into the Options page to programmatically empty the cache when the Options page is saved, as I do not usually give my clients Admin access or bother them with technical things like caches.

The author of ACF says the following:
There is an action called acf/save_post which is used to save all the posted field data. Perhaps you could use this action to hook into and flush the cache.
You could use the $post_id parameter to decide whether it is an options page or not. I believe the options page will pass through ‘0’ as the post_id. Either that or ‘options’.

Is anyone able to help with creating an action that would completely clear the Super Cache cache every time the Options page is saved. This would no doubt help a lot of people!

acf/save_post info here: http://www.advancedcustomfields.com/resources/actions/acfsave_post/

Thanks so much in advance.

Related posts

1 comment

  1. This function will clear WP Super Cache upon saving of ACF Options page. Enjoy!

    <?php
    
    /* Additional Function to prune the Cache if $post_id is '0' or 'options' */
    function f711_clear_custom_cache($post_id) {
    
        // just execute if the $post_id has either of these Values. Skip on Autosave
        if ( ( $post_id == 0 || $post_id == 'options' ) && !defined( 'DOING_AUTOSAVE' ) ) {
    
            // Some Super Cache Stuff
            global $blog_cache_dir;
    
            // Execute the Super Cache clearing, taken from original wp_cache_post_edit.php
            if ( $wp_cache_object_cache ) {
                reset_oc_version();
            } else {
                // Clear the cache. Problem: Due to the combination of different Posts used for the Slider, we have to clear the global Cache. Could result in Performance Issues due to high Server Load while deleting and creating the cache again.
                prune_super_cache( $blog_cache_dir, true );
                prune_super_cache( get_supercache_dir(), true );
            }
        }
    
        return $post_id;
    
    }
    
    // Add the new Function to the 'acf/save_post' Hook. I Use Priority 1 in this case, to be sure to execute the Function
    add_action('acf/save_post', 'f711_clear_custom_cache', 1);
    
    ?>
    

Comments are closed.