#34569 closed enhancement (fixed)
General purpose plugin uninstall hook
Reported by: | eclev91 | Owned by: | 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)
Change History (10)
#2
follow-up:
↓ 3
@
9 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
@
9 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
@
9 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?
#5
@
9 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
@
9 years ago
- Focuses docs added; administration removed
- Milestone changed from Future Release to 4.5
The
'uninstall_' . $file
can't be deprecated. It's essential for plugins that want to register uninstall functionality (though using anuninstall.php
file is encouraged. Seeregister_uninstall_hook()
.If really needed and useful, a
pre_uninstall
action could be added. For your use case, hooking into thepre_update_option_uninstall_plugins
hook should work though.