I would like to hook into a widget instance after it is removed from a sidebar panel on the Appearances -> Widgets admin page.
So if the instance was an active widget and then removed, I would like to hook into that instance before it is removed.
Can it be done?
Yes, this can be done. Ultimately, adding and removing a widget to/from a sidebar means updating an option in the database with a call like this:
If you look at the code of
update_option
you see that there is a filter called just before the actual update. So if you want to do something before a widget is removed you can hook into:At this point you only know that the list of active widgets is going to change, not whether it is addition or a removal. However, you do have
$value
and$old_value
of the option, which you can compare inside your filter function (anarray_search
for a widget’s label might be enough).Read the answer to this question to learn about the structure of the
sidebars_widgets
option. It depends on your goal what you actually want to detect and do.