Make WordPress Core

Ticket #5625: sample_uninstallable_plugin.php

File sample_uninstallable_plugin.php, 3.1 KB (added by arickmann, 17 years ago)

A sample plugin that implements the uninstall process.

Line 
1<?php
2/*
3Plugin Name: Sample Uninstallable Plugin
4Plugin URI: [insert the plugin uri here]
5Description: A sample plugin for testing the uninstaller
6Author: Andrew Rickmann
7Version: 0.1
8Author URI: http://www.wp-fun.co.uk
9Generated At: www.wp-fun.co.uk;
10*/ 
11
12if ( !is_plugin_active( __FILE__ ) ) {
13
14//THIS IS THE UNINSTALL FUNCTIONS
15
16        class fw_uninstallation_sample_uninstall {
17       
18                function uninstall_me(){
19                        global $wpdb;
20       
21                        $db_table_name = $wpdb->prefix . "sample_uninstallable_plugin";
22                       
23                        $sql = 'DROP TABLE IF EXISTS '.$db_table_name;
24                        $wpdb->query( $sql );
25                       
26                        delete_option( "sample_uninstallable_plugin_db_version" );
27       
28        }
29
30}
31
32return;
33
34}
35
36
37
38//start of the plugin as normal
39
40if (!class_exists('fw_uninstallation_sample')) {
41    class fw_uninstallation_sample      {
42               
43                /**
44                * @var string   The name of the database table used by the plugin
45                */     
46                var $db_table_name = '';
47
48               
49                /**
50                * PHP 4 Compatible Constructor
51                */
52                function fw_uninstallation_sample(){$this->__construct();}
53               
54                /**
55                * PHP 5 Constructor
56                */             
57                function __construct(){
58                        global $wpdb;
59
60
61                register_activation_hook(__FILE__,array(&$this,"install_on_activation"));
62               
63                $this->db_table_name = $wpdb->prefix . "sample_uninstallable_plugin";
64
65                }
66               
67
68               
69                /**
70                * Creates or updates the database table, and adds a database table version number to the WordPress options.
71                */
72                function install_on_activation() {
73                        global $wpdb;
74                       
75                        //**************************** UNINSTALLER
76                        register_uninstall_hook( 'uninstall_sample' , __FILE__ , array( 'fw_uninstallation_sample_uninstall' , 'uninstall_me' ) );
77                        //*******************************************
78                       
79                       
80                        $plugin_db_version = "0.1";
81                        $installed_ver = get_option( "sample_uninstallable_plugin_db_version" );
82                        //only run installation if not installed or if previous version installed
83                        if ($installed_ver === false || $installed_ver != $plugin_db_version) {
84               
85                                //*****************************************************************************************
86                                // Create the sql - You will need to edit this to include the columns you need
87                                // Using the dbdelta function to allow the table to be updated if this is an update.
88                                // Read the limitations of the dbdelta function here: http://codex.wordpress.org/Creating_Tables_with_Plugins
89                                // remember to update the version number every time you want to make a change.
90                                //*****************************************************************************************
91                                $sql = "CREATE TABLE " . $this->db_table_name . " (
92                                id mediumint(9) NOT NULL AUTO_INCREMENT,
93                                test_field VARCHAR(255),
94                                UNIQUE KEY id (id)
95                                );";
96                       
97                                require_once(ABSPATH . "wp-admin/upgrade-functions.php");
98                                dbDelta($sql);
99                                //add a database version number for future upgrade purposes
100                                update_option("sample_uninstallable_plugin_db_version", $plugin_db_version);
101                        }
102                }
103
104    }
105}
106
107//instantiate the class
108if (class_exists('fw_uninstallation_sample')) {
109        $fw_uninstallation_sample = new fw_uninstallation_sample();
110}
111
112
113
114?>