Fun with Uninstallation 2
After reading the comments on my first uninstallation plugin attempt, and the WordPress Trac, it become clear that the approach of using an external file probably wasn’t going to cut it. So I have taken that on board and produced a revised version.
I think version 0.2 is a significant step forward, not least because it solves the problem of needing a separate file.
So how does this version work?
The plugin author needs to do two things. The first is to add an uninstall option at the top of the plugin file, somthing like this:
if ( !is_plugin_active( __FILE__ ) ) { class my_plugin{ function uninstall(){ } } return; }
What this does is to only expose the uninstall methods if the plugin is not active, and further, the addition of the return at the end stops the rest of the plugin being loaded.
Important Note: An inactive plugin is only loaded if it is in the process of being uninstalled. It will not be loaded at any other time and so won’t impact on performance or security any more than they already do.
The second thing the plugin author needs to do is to register the uninstall function. This should be done when the plugin is activated. The function that needs to be called is:
register_uninstall_hook( ‘uninstall_sample’ , __FILE__ , array( ‘my_plugin’ , ‘uninstall’ ) );
The first argument is a friendly name, the second is the plugin url, the third is the uninstall function itself. It is essential to remember that the class is not the main plugin class, but the one inside the block that runs if the plugin in not installed, so using &$this isn’t going to work.
What happens, when?
when the plugin is activated it is added to a list of uninstallable plugins in a database option.
The plugins page checks whether a plugin is active, if it is not and it is in the list of uninstallable plugins it offers the uninstall option.
If you select the unstall option it loads the plugin, activates the hook, and removes the plugin from the list of uninstallable plugins so that the option to uninstall is no longer shown.
The zip file below includes 0.2 of the plugin and another sample plugin that has the uninstall instructions. On activation it creates a database table, and cleans it up when it is uninstalled.
You can download it here: Fun with uninstallation 2
Update I am most of the way through version 0.3. 0.3 will emulate the patch I intend to submit to the trac later on today. It will work a little differently to this, automating more of the uninstallation process. Full details will follow later on.
Details of version three can be found here
Comments
Other blogs writing about this
-
(http://www.wp-fun.co.uk/2008/01/09/fun-with-uninstallation/) 11th 01 2008 at 8:41 pm
[...] 2: This has now been superseded by this post, fun with uninstallation 2 [...]
-
(http://weblogtoolscollection.com/archives/2008/01/09/wordpress-plugin-uninstall-tool/) 11th 01 2008 at 11:51 pm
[...] the plugins page.  Update Jan 11: Andrew has released a second iteration of the uninstall tool. Click here to check it out. (18 votes, average: 4.94 out of 5) Loading … Sphere: Related [...]
-
(http://www.jeffro2pt0.com/wordpress-weekly-episode-2/) 07th 02 2008 at 10:54 pm
[...] Andrew Rickmann - An Uninstall tool that adds an uninstallation option on the plugins page. The uninstall option will only show up if the plugin has been deactivated and if the plugin author has created an uninstall file. Check out the second iteration of his tool, here. [...]
Leave a Reply
I am currently testing a comment link policy which means commenters do not get a link. There is a poll, and open comments for feedback on the comment policy page.

1
James (http://www.jamesturner.co.nz) commented at 8:30 am, 13th 01 2008:
Great idea! Keep up the great work. I hate looking at my localhost DB and seeing the mess that is left after looking for a great plugin.
=-)