这可能是一个相当复杂的问题,因此我的回答可能不太严谨,但这应该会给您在创建和删除表时提供一个良好的开端。。
在class\\u my\\u plugin的plugin \\u construct()函数中包括以下几行。php:
if (is_admin()){
    //manage the the database tables when registering or deactivating
    register_activation_hook($loc, array($this, \'your_plugin_installation\'));
    register_deactivation_hook($loc, array($this, \'your_plugin_unset\'));
    register_uninstall_hook ($loc, \'your_plugin_uninstall\');
}
 在class\\u my\\u plugin中包含这些函数。类本身中的php:
function your_plugin_installation() {
    create_data_tables();
}
function your_plugin_unset() {
    //do whatever here for users that deactivate the plugin
}
 在主插件类之外添加以下函数:
function your_plugin_uninstall() {
    delete_db_tables();
    //add whatever else you need to do to clean up
}
function create_data_tables() {
    global $wpdb;
    require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
    $table_name = $this->table_prefix . \'desks\';
    $charset_collate = $wpdb->get_charset_collate();
    if( $wpdb->get_var("SHOW TABLES LIKE \'".$table_name."\'") != $table_name ) {
       $sql = "CREATE TABLE IF NOT EXISTS ".$table_name;
       $sql .= " (some_id int(11) UNSIGNED NOT NULL AUTO_INCREMENT, ";
       $sql .= "some_title varchar(31) NOT NULL DEFAULT \'\', ";
       $sql .= "PRIMARY KEY (some_id)) ".$charset_collate;
        dbDelta( $sql );
    }
    // repeat the if {} statement for each table you want to create
}
function delete_db_tables() {
    global $wpdb;
    $table_name = $this->table_prefix . \'desks\';
    $wpdb->query( "DROP TABLE IF EXISTS ".$table_name );
    //delete your tables here
}
 请注意,我通常将您的\\u plugin\\u卸载放在主类之外,因为当用户卸载时,如果它位于主类之内,则会出现错误。
这将帮助您在用户安装和卸载时创建和删除表。因此,这只会发生一次,除非启动安装或卸载挂钩,否则这些函数将被忽略。
可以使用标准WP功能轻松访问这些表,例如:。
function so_get_some_title($id,) {
    global $wpdb;
    $id = (int) $id;
    $query = "SELECT some_id, some_title FROM ".$table_name." WHERE some_id = ".$id;
    $recs = $wpdb->get_results($query,ARRAY_A);
    }
    return $recs;
}
 我希望我正确地解释了你的问题,这在某种程度上有所帮助。