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 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开