Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#34569 closed enhancement (fixed)

General purpose plugin uninstall hook

Reported by: eclev91's profile eclev91 Owned by: swissspidy's profile swissspidy
Milestone: 4.5 Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch commit
Focuses: docs Cc:

Description

When you install or upgrade a plugin, there's a convenient hook (upgrader_process_complete) that provides a pile of information about what happened. You can subscribe to any install or upgrade and then use the information you've been handed to decide if something needs to be done.

No such thing for uninstall. Looks like the best you can get is in the uninstall_plugin() function, line 990 of wp-admin/includes/plugin.php:

do_action( 'uninstall_' . $file );

Where $file is the plugin's main file name. This requires you to know what plugin you're looking for if you hook on.

Use case:
I manage all my WP installs via Composer. When I install a plugin via the admin, I can hook in and update my composer.json. It'd be nice to be able to update on uninstall as well.

The current action could be left for backwards compatibility and a new one written, or the old one could be dropped, as this new one would allow you to determine what plugin was uninstalled and do something if necessary, just like the old one.

I could work on a PR, but thought I'd get feedback on deprecation process for the old one, if that's the route we want to go.

Attachments (1)

34569.diff (854 bytes) - added by swissspidy 8 years ago.

Download all attachments as: .zip

Change History (10)

#1 @swissspidy
8 years ago

The 'uninstall_' . $file can't be deprecated. It's essential for plugins that want to register uninstall functionality (though using an uninstall.php file is encouraged. See register_uninstall_hook().

If really needed and useful, a pre_uninstall action could be added. For your use case, hooking into the pre_update_option_uninstall_plugins hook should work though.

#2 follow-up: @eclev91
8 years ago

I just searched the WordPress core files (and Google) - I don't see pre_update_option_uninstall_plugins anywhere. Can you elaborate?

#3 in reply to: ↑ 2 @DrewAPicture
8 years ago

Replying to eclev91:

I just searched the WordPress core files (and Google) - I don't see pre_update_option_uninstall_plugins anywhere. Can you elaborate?

It's a dynamic hook, pre_update_option_{$option}, where $option is "uninstall_plugins".

See here: https://developer.wordpress.org/reference/hooks/pre_update_option_option/

#4 @eclev91
8 years ago

Looking at this, it seems like I'd have to hook to that action to get all the plugins that could be uninstalled, and then add an uninstall_$file action to listen for each of those files to catch if the plugin truly was uninstalled.

Alternatively, hook into update_option_uninstall_plugins, which gives me both the new and old values, figure out which ones are missing, and count those as uninstalled.

Sound about right?

@swissspidy
8 years ago

#5 @swissspidy
8 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to Future Release
  • Version 4.3.1 deleted

34569.diff adds a pre_uninstall_plugin hook that runs before the plugin is uninstalled.

This makes it easier for people to hook into uninstall_plugin(). If you're not aware of the pre_update_option_uninstall_plugins hook or that hook ever changes, the proposed addition makes it more transparent.

#6 @swissspidy
8 years ago

  • Focuses docs added; administration removed
  • Milestone changed from Future Release to 4.5

#7 @swissspidy
8 years ago

  • Keywords commit added

#8 @swissspidy
8 years ago

  • Owner set to swissspidy
  • Resolution set to fixed
  • Status changed from new to closed

In 36124:

Plugins: Add a new pre_uninstall_plugin action that runs right before uninstalling a plugin.

Fixes #34569.

#9 @DrewAPicture
8 years ago

In 36824:

Docs: Minorly improve the hook doc summary for the pre_uninstall_plugin action, introduced in [36124].

See #34569. See #35986.

Note: See TracTickets for help on using tickets.