Make WordPress Core

Opened 10 years ago

Closed 8 years ago

#27444 closed enhancement (fixed)

delete widget action hook

Reported by: afercia's profile afercia Owned by: drewapicture's profile DrewAPicture
Milestone: 4.4 Priority: normal
Severity: normal Version: 3.8
Component: Widgets Keywords: has-patch needs-testing
Focuses: Cc:

Description

Just a proposal to have a new action to hook in when deleting a widget.

For example, many themes or plugins provide widgets that fetch data from external sources and then store the response in a transient.
When users delete the widget, the transient won't be deleted and will live forever in the database because it won't be requested again.

Currently, the only way I've found to delete that transient, would be checking if POST 'delete_widget' isset and true and then checking POST 'widget-id'. Same checks are already used in the core but having a specific action to use would be much more clean.

Then, it would be a matter of a few lines of code to add an action to your widget constructor and write a new small method in your widget class to delete the transient. Basically it's something similar to what you're already doing to flush the wp_cache of the default Recent Posts widget.

Besides this specific scenario, there might be lots of things developers would like to do after deleting a widget and this would be very handy.

Attached proposal is just an example, for sure can be improved (maybe pass the widget-id as argument?) and extended. Hope you get the idea. Please consider I'm an absolute beginner with the WP core. Be nice :)

Attachments (2)

27444.patch (1.2 KB) - added by afercia 10 years ago.
27444.2.patch (1.5 KB) - added by DrewAPicture 8 years ago.

Download all attachments as: .zip

Change History (11)

@afercia
10 years ago

#1 @afercia
10 years ago

  • Keywords has-patch needs-testing added

#2 @johnbillion
10 years ago

Sounds sensible. Definitely needs some contextual parameters passed to the action too, such as $id_base and $sidebar_id.

#3 @SergeyBiryukov
10 years ago

  • Version changed from trunk to 3.8

#4 @SergeyBiryukov
10 years ago

  • Keywords 4.0-early added
  • Milestone changed from Awaiting Review to Future Release

#5 in reply to: ↑ description @afercia
10 years ago

thx John, Sergey,

When users delete the widget, the transient won't be deleted and will live forever in the database because it won't be requested again.

Actually, if expired it will be deleted just on database upgrade (#20316)

Last edited 10 years ago by SergeyBiryukov (previous) (diff)

#6 @wonderboymusic
9 years ago

  • Owner set to DrewAPicture
  • Status changed from new to assigned

#7 @DrewAPicture
9 years ago

  • Status changed from assigned to accepted

#8 @DrewAPicture
8 years ago

  • Keywords 4.0-early removed
  • Milestone changed from Future Release to 4.4

#9 @DrewAPicture
8 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 34714:

Widgets: Introduce a delete_widget action, which fires after a widget has been marked for deletion.

Props afercia for the initial patch.
Fixes #27444.

Note: See TracTickets for help on using tickets.