创建 table 自定义插件时 Wordpress 插入数据

Wordpress insert data when creating table custom plugin

您好,我正在 wordpress

中创建一个 table
        $sql = "CREATE TABLE IF NOT EXISTS $wpdb->prefix" . "profil_member(
            id_profil bigint(20)UNSIGNED NOT NULL AUTO_INCREMENT,
            id_member bigint(20) UNSIGNED NOT NULL,
            id_subscription bigint(20) UNSIGNED NOT NULL,
            createdAt DATETIME DEFAULT CURRENT_TIMESTAMP,
            updatedAt DATETIME,
            state int DEFAULT 1,
            PRIMARY KEY(id_member,id_subscription),
            FOREIGN KEY (id_profil) REFERENCES $wpdb->prefix" . "profil(id),
            FOREIGN KEY (id_member) REFERENCES $wpdb->prefix" . "member(id)
            ) " . $wpdb->get_charset_collate();

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);

而且我想在使用来自 antoher table

的日期创建时填充此 table
       if (version_compare($version, '1.7') < 0) {
            $this->populate_profil_member();
            update_option('my_plugin_version', '1.7');
        }

当前插件版本为 1.6。

如果我用 1.7 更新加载插件,它会创建 table 但不插入数据并将插件更新到 1.7

如果我使用创建 table 而不是插入加载插件,它会创建 table 然后如果我使用更新重新加载它插入数据并更新到 1.7

有没有一种解决方案可以只创建 table 并同时插入数据。 ?

按照这个例子:

function insert_data_into_database() {
    global $wpdb;
    
    $welcome_name = 'Mr. WordPress';
    $welcome_text = 'Congratulations, you just completed the installation!';
    
    $table_name = $wpdb->prefix . 'your-table-name';
    
    $wpdb->insert( 
        $table_name, 
        array( 
            'time' => current_time( 'mysql' ), 
            'name' => $welcome_name, 
            'text' => $welcome_text, 
        ) 
    );
}