I’m making a WordPress plugin. What are typical things I should include in the uninstall feature?
For example, should I delete any tables I created in the install function?
Do I clean up my option entries?
Anything else?
I’m making a WordPress plugin. What are typical things I should include in the uninstall feature?
For example, should I delete any tables I created in the install function?
Do I clean up my option entries?
Anything else?
You must be logged in to post a comment.
There are three different hooks. They trigger in the following cases:
How-to trigger functions safely during the scenarios
The following shows the right ways to safely hook callback functions that get triggered during the mentioned actions.
As you could use this code in a plugin that uses
I’ll show three different demo plugins that you can inspect and then later on implement the code in your own plugin(s).
Important note upfront!
As this topic is extremely difficult and very detailed and has a dozen+ edge cases, this answer will never be perfect. I’ll keep improving it over time, so check back on a regular base.
(1) Activate/Deactivate/Uninstall plugins.
The plugin setup callbacks are triggered by core and you have no influence on how core does this. There’re some things to keep in mind:
echo/print
anything(!) during setup callbacks. This will lead toheaders already sent
message and core will recommend to deactivate and delete your plugin… don’t ask: I know…exit()
statements to all different callbacks so you can get some insights on what is really happening. Just uncomment them to see stuff working.__FILE__ != WP_PLUGIN_INSTALL
and (if not: abort!) to see if one is really uninstalling the plugin. I’d recommend to simply triggeron_deactivation()
callbacks during development, so you save yourself the time that you’d need to get everything back in. At least this is what I do.defined( 'ABSPATH' ) OR exit;
wp_die()
screen asking for proper permissions (and if you want to try again … yeah, sure), when you got an error. This happens as core redirects you, sets the current$GLOBALS['wp_list_table']->current_action();
toerror_scrape
and then checks the referrer forcheck_admin_referer('plugin-activation-error_' . $plugin);
, where$plugin
is$_REQUEST['plugin']
. So the redirect happens at half the page load and you get this wired scroll bar and the die screen insight the yellow admin notice/message box. If this happens: Stay calm and just search for the error with someexit()
and step-by-step debugging.(A) Plain functions plugin
Remember that this might not work if you hook the callbacks before the function definition.
(B) A class based/OOP architecture
This is the most common example in nowadays plugins.
(C) A class based/OOP architecture with an external setup object
This scenario assumes that you got a main plugin file and a second file named
setup.php
in a subdirectory of the plugin namedinc
:~/wp-content/plugins/your_plugin/inc/setup.php
. This will work as well when the plugin folder is outside the default WP folder structure, as well when the content dir is renamed or in cases where your setup file is named different. Only theinc
folder has to have the same name & location relative from the plugins root directory.Note: You can simply take the three
register_*_hook()*
functions and the classes and drop them into your plugin.The main plugin file:
The setup file:
(2) Plugin updates
If you write a plugin that has its own DB table or options, there might be scenarios where you need to change or upgrade things.
Sadly so far there’s no possibility to run something on plugin/theme install or update/upgrade. Gladly there’s a work-around: Hook a custom function to a custom option (yea, it’s lame – but it works).
Source
This update function is a not-so-nice/well-written example, but as said: It’s an example and the technique works well. Will improve that with a later update.
To test the current system for required featurs like PHP version or installed extensions you can use something like that:
Test with a check for PHP 5.5: