douju8113 2013-04-24 05:37
浏览 66
已采纳

我的自定义表没有在wordpress插件激活上创建

I am trying to create my own custom table in wordpress database on plugin activation ..This is my code for that ..

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);

   }
}

I m checking my database .No new table has been created ..Plz look into it ..

  • 写回答

1条回答 默认 最新

  • dongwen1935 2013-04-24 05:39
    关注

    That's because any WP activation/deactivation hook needs to be run from inside the plugin main file, not from a file you include in the main file. So, try to run the activation hook from your plugin-name.php file and it will work.

    L.E:
    also, i don't see $divebook_db_table_dive_version being defined. Another thing, dbDelta caused problems for me in the past, try with $wpdb->query() to run the create table query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探