1

我正在尝试在插件激活时在 wordpress 数据库中创建自己的自定义表..这是我的代码..

function __construct()
    {
register_activation_hook(__FILE__,array(&$this, 'activate'));


    function activate()
    {

    global $wpdb;
    echo "<div class='updated'>Test Plugin Notice</div>";

    $table_name = $wpdb->prefix . "dive";

    $installed_ver = get_option( "divebook_db_table_dive_version" );
     //Check if the table already exists and if the table is up to date, if not create it
    if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name
            ||  $installed_ver != $divebook_db_table_dive_version ) {
        $sql = "CREATE TABLE " . $table_name . " (
              id mediumint(9) NOT NULL AUTO_INCREMENT,
              date bigint(11) DEFAULT '0' NOT NULL,
              site tinytext NOT NULL,
              description text NOT NULL,
              max_depth mediumint(9) NOT NULL,
              time mediumint(9) NOT NULL,
              UNIQUE KEY id (id)
            );";

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

}
    //Add database table versions to options
    add_option("divebook_db_table_dive_version", $divebook_db_table_dive_version);

   }
}

我正在检查我的数据库。没有创建新表..请查看它..

4

2 回答 2

1

这是因为任何 WP 激活/停用挂钩都需要从插件主文件内部运行,而不是从您包含在主文件中的文件中运行。因此,尝试从您的 plugin-name.php 文件运行激活钩子,它将起作用。

LE:
另外,我没有看到 $divebook_db_table_dive_version 被定义。另一件事,dbDelta 过去给我带来了问题,尝试使用 $wpdb->query() 来运行创建表查询。

于 2013-04-24T05:39:35.663 回答
0

可能是因为 SQL 查询不正确,只需查看此处的文档 https://codex.wordpress.org/Creating_Tables_with_Plugins

于 2020-09-15T19:14:47.187 回答