• Resolved Liddika

    (@liddika)


    Hi,

    Is there a hook I can add to the “Purge All Hooks” that gets triggered whenever a widget (inside of Appearance -> Widgets) gets added, removed from a widget area, or when widgets get rearranged, or when a widget gets saved with new settings.

    So far I could only find delete_widget, but nothing for when adding a widget, rearranging or when updating a setting of an existing widgets.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support qtwrk

    (@qtwrk)

    Thread Starter Liddika

    (@liddika)

    Thank you. I had a look and ran a few tests, however for some reason that hook wasn’t always fired and I couldn’t figure out why.

    Here is what I came up with:

    When a widget is added, removed or saved, the update_option hook is fired.

    All widget option names start with “widget_“. By checking if the option name starts with “widget_” we can determine if a widget was added, removed or saved.

    When widgets are being reordered within a widget area, the new positions are being saved to the option “sidebars_widgets“.

    The hook “updated_option” fires immediately after an option has been successfully updated. Note: “updated_option” will not fire if the old value and the new value are the same.

    function purge_cache_on_widget_change( $option, $old_value, $value ) {
        if( substr( $option, 0, 7 ) === 'widget_' || $option === 'sidebars_widgets' ) {
            if ( is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
                do_action( 'litespeed_purge_all' );
            }
        }
    }
    add_action('updated_option','purge_cache_on_widget_change', 10, 3);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Auto purge cache when adding, removing or updating widgets?’ is closed to new replies.