6

I am really making a huge use of ACF's options pages and repeater fields. In order to reduce the amount of queries (from about 500 to 80) I'm caching some field outputs with the help of Wordpress Transient API.

I know the hook for ACF options pages is:

add_action( 'acf/save_post', 'reference_function') ); 

But the problem for me is, that I have multiple options pages. And I don't want all my functions to run when any options page is saved…

These are my current options pages

add_filter('acf/options_page/settings', 'my_acf_options_page_settings');
if(function_exists("register_options_page")) {
        acf_add_options_page();
        register_options_page('Spielübersicht');
        register_options_page('Mannschaft');
        register_options_page('SCHWALBE arena TV');
        register_options_page('Sponsoren');
        register_options_page('Werbung');
        register_options_page('Einstellung');
        register_options_page('Fanclubs');
        register_options_page('Sidebar');
    }

Is there a way to filter the action so that my transients are only created when the related options page is saved?

cruzquer
  • 301
  • 1
  • 3
  • 11

2 Answers2

9

Luckily I was able to solve my problem! Have a look at my code in the plugins’ support forum: http://support.advancedcustomfields.com/forums/topic/acfsave_post-for-specific-options-page/

function clear_advert_main_transient() {
    $screen = get_current_screen();
    if (strpos($screen->id, "acf-options-adverts") == true) {
        delete_transient('advert_main_transient1');
        delete_transient('advert_main_transient2');
        delete_transient('advert_main_transient3');
    }
}
add_action('acf/save_post', 'clear_advert_main_transient', 20);
cruzquer
  • 301
  • 1
  • 3
  • 11
0

I know this question is a bit old, but seems like (finally) the ACF Pro has a built in action for doing this.

Since version 6.1.7 we could do this:

add_action('acf/options_page/save', 'my_acf_save_options_page', 10, 2);
function 'my_acf_save_options_page'( $post_id, $menu_slug ) {

    if ( 'theme-settings' !== $menu_slug ) {
        return;     
    }
    // Get newly saved values for the theme settings page.
    $values = get_fields( $post_id );

    // Check the new value of a specific field.
    $analytics_code = get_field('analytics_code', $post_id);
    if( $analytics_code ) {
        // Do something...
    }
}

Official documentation: https://www.advancedcustomfields.com/resources/acf-options_page-save/