I ended up using the updated_option
hook and checking if a widget or sidebar has been updated.
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' ) {
my_function_purge_cache();
}
}
add_action('updated_option','purge_cache_on_widget_change', 10, 3);