我制作了一个使用数据库的插件,其中包含如下所示的表:
register_activation_hook( __FILE__, 'plugin_install' );
function plugin_install() {
global $wpdb;
$table1 = $wpdb->prefix . 'table1';
$charset_collate = '';
if ( ! empty( $wpdb->charset ) ) {
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}
if ( ! empty( $wpdb->collate ) ) {
$charset_collate .= " COLLATE {$wpdb->collate}";
}
$sql = "CREATE TABLE IF NOT EXISTS $table1 (
id mediumint(9) NOT NULL AUTO_INCREMENT,
some_data varchar(55) DEFAULT '' NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
当通过管理面板删除插件时,我不知道如何删除所有这些表格。有人可以帮忙吗?
答案 0 :(得分:0)
在另一方面做同样的事:register_uninstall_hook($file, $callback)
http://codex.wordpress.org/Function_Reference/register_uninstall_hook
例如:
register_uninstall_hook(__FILE__, 'plugin_uninstall');
function plugin_uninstall() {
global $wpdb;
$table1 = $wpdb->prefix . 'table1';
$sql = "DROP TABLE " . $table1;
dbDelta($sql);
}