logo
  • Home
  • About
  • Plugins

Subscribe to Articles

Fun with Uninstallation 2

Author: andrew Category: Uncategorized Tags: plugins

Friday
Jan 11, 2008

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


Share:
image image image image image

Comments

James (http://www.jamesturner.co.nz)

January 13th, 2008 at 8:30 am

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.
=-)


Fun with Uninstallation
CSS Theme Standards

Recommended

WP Remix Banner

Archives

  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007

Tags

2.7 2.8 Admin Advertising Air Blogging blogs Cache Cliche Coding Comments Content Types CSS Curry Death development Disqus Features Habari image Interview Licensing Modes Monday Poll New Features Organisation Personal Platform Plugin Plugin-Practices plugins Plugin update Poll premium Readers Reviews Search Simplification Snippets Spam Themery Themes tools User Interface Widgets WLTC

Copyright 2009 Fun with WordPress - All Rights reserved.

Wordpress theme by: WPUnlimited