Install/Uninstall Actions

 

You may want certain actions to happen during installation and uninstall. A typical example is the creation/deletion of a database table for the plugin.

Your XXX_Plugin class inherits install() and uninstall() functions from its superclass XXX_LifeCycle. DO NOT OVERRIDE these functions. Instead, look at the code. Each one calls other functions defined in XXX_LifeCycle that you can override in your XXX_Plugin class.

First, look at install:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    public function install() {
 
        // Initialize Plugin Options
        $this->initOptions();
 
        // Initialize DB Tables used by the plugin
        $this->installDatabaseTables();
 
        // Other Plugin initialization - for the plugin writer to override as needed
        $this->otherInstall();
 
        // Record the installed version
        $this->saveInstalledVersion();
 
        // To avoid running install() more then once
        $this->markAsInstalled();
    }
 
    public function uninstall() {
        $this->otherUninstall();
        $this->unInstallDatabaseTables();
        $this->deleteSavedOptions();
        $this->markAsUnInstalled();
    }

Override installDatabaseTables() and otherInstall() as needed.

1
2
    protected function unInstallDatabaseTables() {
    }
1
2
    protected function otherInstall() {
    }

Looking at uninstall:

1
2
3
4
5
6
    public function uninstall() {
        $this->otherUninstall();
        $this->unInstallDatabaseTables();
        $this->deleteSavedOptions();
        $this->markAsUnInstalled();
    }

Override otherUninstall and uninstallDatabaseTables() as needed

1
2
    protected function otherUninstall() {
    }
1
2
    protected function unInstallDatabaseTables() {
    }

  14 Responses to “Install/Uninstall Actions”

  1. I’m going slightly cross-eyed as to why my tables aren’t being created. I’ve tested in another plugin and it works fine. It is like installDatabaseTables() isn’t being called

    • Best would be to attach the proper hook to the event:

      I call this in the plugin init:
      $ur_plugin = new XXX_Plugin([]);
      // Register the Plugin Uninstall Hook
      register_uninstall_hook($file, array(&$ur_plugin, ‘uninstall’));

  2. In the XXX_Plugin.php, the installDatabaseTables are declared as “protected.” Changing it to “public” calls the function. The same is true for the unInstallDatabaseTables.

    Happy programming.

  3. In WP 4.0 the installDatabaseTables() of XXX_Plugin.php executes Ok and the options are added, but in the uninstall the unInstallDatabaseTables() function never executes and the options remains set in database.
    Any idea???

    • I neither found any call to uninstall.
      My guess is that many time when user deactivate a plugin he does not want to loose configuration and settings.
      In my case I want to leave the wp installation clean, so in XXX_LifeCycle.php I added this code:

      public function deactivate() {
      $this->uninstall();
      }

  4. In file XXX_Plugin, inside the function initOptions() I found this line, which looks like a bug:

    if (is_array($arr) && count($arr > 1)) {

    IMO it should be:

    if (is_array($arr) && count($arr) > 1) {

  5. Hi,

    I’m using your plugin. I’ve created a CURL request in the activate function which contacts an API that sends me an email every time it gets triggered. The API is a remote API.

    The problem is that i receive 3 emails.
    So it seems to me that the activate function is being called three times.
    The problem is that I can’t find where it does this.

    Can you help me on this?

    Best regards.

  6. The uninstall() function is not being called form any place, that’s what causing it not to drop any DB tables.

  7. I’ve added the uninstall code to deactivate() function inside XXX_LifeCycle.php and now it works like a charm:
    Before

    public function deactivate() {
    }

    After

    public function deactivate() {
    $this->uninstall();
    }

  8. If you do not want the database to be dropped after a deactivate, but only after an actual uninstall, better use the proper hook in _Init.php

    I call this in the plugin init:
    $ur_plugin = new XXX_Plugin([]);
    // Register the Plugin Uninstall Hook
    register_uninstall_hook($file, array(&$ur_plugin, ‘uninstall’));

  9. In above code second code snippet is for uninstall that should be for installTables i think..

  10. This is a well-written template – Thank you. But the Table creating is a problem. Cannot work out why tables are not created. Checked the syntax – no error reported. Checked the functions and everything. Can someone help

Leave a Reply to Mike Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>