2

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…

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.

closed as off-topic by birgire, Pieter Goosen, gmazzap, Johannes Pille, kaiser Sep 22 '14 at 20:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – birgire, Pieter Goosen, gmazzap, Johannes Pille, kaiser
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I don't know the codebase but if you look in the theme at its hooks into wp_edit_post and specifically the function wp_cache_post_edit in the wp-cache-phase2.php file, you'll find how it hooks into the edit interface to clear its cache. You should be able to use the same hooks to clear your cache on the acf/save_post action. – George Pearce May 6 '13 at 19:29
  • Thanks George, I've pasted the code from wp_cache_post_edit below, would you know how to make it work? – Jacob May 7 '13 at 19:50
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);

?>

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