Ich fange gerade an, mein erstes WP-Plugin zu schreiben (genauer gesagt um hierfür eine Lösung zu entwickeln). Dazu benötige ich ein paar eigene Datenbanktabellen, die ich in einer Installationsroutine erzeugen und natürlich genauso wiederum in einer Deinstallationsroutine wieder entfernen möchte.
Hier auf WP.org wird dies bezüglich empfohlen, die Funktion dbDelta($sql) dazu zu verwenden. Das tue ich dann auch, treffe aber auf folgende Probleme:
1. Installation: Von meinen drei CREATE TABLE-Anfragen wird offenbar nur eine ausgeführt. Jedenfalls sehe ich nach aktivieren meines Plugins nur eine neue Datenbanktabelle per PhpMyAdmin. Die anderen beiden, die eigentlich auch angelegt werden sollten, fehlen.
2. Deinstallation: Beim Löschen meines Plugins sollten die zuvor angelegten Tabellen eigentlich alle sauber wieder gelöscht werden. Allerdings passiert das nicht. Die Deinstallationsroutine läuft aber definitiv, da wiederum ein per delete_option($str) zu entfernender Konfigurationswert definitiv aus der Datenbank wieder verschwunden ist.
Hier mein Code (Kommentare zwecks Übersichtlichkeit gelöscht):
global $wpdb;
define("DASHBASETABLEPRE", $wpdb->prefix . "dashbaseplugin_");
define("DASHBASETABLECAT", DASHBASETABLEPRE . "category");
define("DASHBASETABLEPOST", DASHBASETABLEPRE . "article");
define("DASHBASETABLESTRUCT", DASHBASETABLEPRE . "structure");
define("DASHBASE_VERSION", "0.1");
register_activation_hook(__FILE__, 'dashbase_install');
register_uninstall_hook(__FILE__, 'dashbase_uninstall');
function dashbase_install() {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$table_cat_name = DASHBASETABLECAT;
$table_post_name = DASHBASETABLEPOST;
$table_structure_name = DASHBASETABLESTRUCT;
if($wpdb->get_var("SHOW TABLES LIKE '$table_cat_name'") != $table_cat_name) {
$sql = "CREATE TABLE " . $table_cat_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
title tinytext NOT NULL default 'Category',
description text,
weight mediumint(9) NOT NULL default 0,
roles SET('admin','editor','author','contributor','subscriber'),
UNIQUE KEY (id)
);";
dbDelta($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$table_post_name'") != $table_post_name) {
$sql = "CREATE TABLE " . $table_post_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
title tinytext NOT NULL default 'Category',
body mediumtext,
UNIQUE KEY (id)
);";
dbDelta($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$table_structure_name'") != $table_structure_name) {
$sql = "CREATE TABLE " . $table_structure_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
cid mediumint(9) NOT NULL,
pid mediumint(9) NOT NULL,
weigth mediumint(9) NOT NULL default 0,
roles SET('admin','editor','author','contributor','subscriber'),
PRIMARY KEY (id),
UNIQUE KEY structure (cid,pid)
);";
dbDelta($sql);
}
add_option("dashbase_version", DASHBASE_VERSION);
}
function dashbase_uninstall() {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$sql = "DROP TABLE IF EXISTS " . DASHBASETABLECAT . ", " . DASHBASETABLEPOST . ", " . DASHBASETABLESTRUCT . ";";
dbDelta($sql);
delete_option("dashbase_version");
}
Alles anzeigen