插件激活时创建表

时间:2014-12-19 作者:Nurul

// Initialization and Hooks
global $wpdb;
global $wp_version;
global $wpmyfirstplugin_version;
global $wpmyfirstplugin_db_version;
global $wpmyfirstplugin_table_name;
global $wp_version;
$wpmyfirstplugin_version = \'1.0.1\';
$wpmyfirstplugin_db_version = \'0.0.1\';
$wpmyfirstplugin_table_name = $wpdb->prefix.\'imart_setting\';

function wpmyfirstplugin_install()
{
    global $wpdb;
    global $wpmyfirstplugin_table_name;
    global $wpmyfirstplugin_db_version;

    // create table on first install
    if($wpdb->get_var("show tables like \'$wpmyfirstplugin_table_name\'") != $wpmyfirstplugin_table_name) {
        wpmyfirstplugin_createTable($wpdb, $wpmyfirstplugin_table_name);

    //add_option("wpmyfirstplugin_db_version", $wpmyfirstplugin_db_version);
        //add_option("wpmyfirstplugin_configuration", \'\');
    }

}

function wpmyfirstplugin_createTable($wpdb, $table_name)
{
    $sql = "CREATE TABLE  ".$table_name." (
          main_id bigint(20) NOT NULL auto_increment,
          main_key varchar(255) default NULL,
          main_value longtext,
          PRIMARY KEY  (`imart_id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";

    $results = $wpdb->query($sql);
}


register_activation_hook(__FILE__,\'wpmyfirstplugin_createTable\');
以上是我为starter输入的内容。当我激活时,我得到以下信息:

警告:wpmyfirstplugin\\u createTable()缺少参数2

这一点指向wpmyfirstplugin_createTable

wpmyfirstplugin_createTable($wpdb, $wpmyfirstplugin_table_name);
致命错误:对非对象调用成员函数query()

这是我跑步的地方

$results = $wpdb->query($sql);
我到底做错了什么?

4 个回复
最合适的回答,由SO网友:Domain 整理而成

出现此错误的原因是在register\\u activation\\u hook上调用了“wpmyfirstplugin\\u createTable”。当从仪表板激活插件时,在声明任何变量之前调用此挂钩。因此,当此函数运行时,它不会获取table name参数。

SO网友:Bruno Moreira

您可以使用:

maybe_create_table( string $table_name, string $create_ddl );
// be sure to include this before calling the function. 
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' ); 

https://developer.wordpress.org/reference/functions/maybe_create_table/

SO网友:user3851648

请尝试此代码:

function test_db_install() 
{
    global $wpdb;
    require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
   $sql="
       CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."imart_setting`
     (
          main_id bigint(20) NOT NULL auto_increment,
          main_key varchar(255) default NULL,
          main_value longtext,
          PRIMARY KEY  (`main_id`)
     );";
    dbDelta($sql);

$insert_sql ="
    INSERT INTO `wp_posts`(`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (\'admin\',\'\',\'\',\'[Alumini Dashboard]\',\'Dashbord\',\'\',\'publish\',\'\',\'\',\'\',\'dashbord\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'\',\'page\',\'\',\'\')";

        dbDelta($insert_sql);

SO网友:sunil

Please Try this

// Initialization and Hooks
global $wpdb;
global $wp_version;
global $wpmyfirstplugin_version;
global $wpmyfirstplugin_db_version;
global $wpmyfirstplugin_table_name;
global $wp_version;
$wpmyfirstplugin_version = \'1.0.1\';
$wpmyfirstplugin_db_version = \'0.0.1\';
$wpmyfirstplugin_table_name = $wpdb->prefix.\'imart_setting\';
function wpmyfirstplugin_install(){
global $wpdb;
global $wpmyfirstplugin_table_name;
global $wpmyfirstplugin_db_version;
// create table on first install
if($wpdb->get_var("show tables like \'$wpmyfirstplugin_table_name\'") != $wpmyfirstplugin_table_name) {
    wpmyfirstplugin_createTable($wpmyfirstplugin_table_name);

}
}
function wpmyfirstplugin_createTable($table_name){
global $wpdb;
$sql = "CREATE TABLE  ".$table_name." (
      main_id bigint(20) NOT NULL auto_increment,
      main_key varchar(255) default NULL,
      main_value longtext,
      PRIMARY KEY  (`imart_id`)
    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";

$results = $wpdb->query($sql);
}
register_activation_hook(__FILE__,\'wpmyfirstplugin_install\');
结束

相关推荐

无法在多站点上查看网络plugins.php或upgrade-core.php

我有一个有5或6个站点的网络。在网络管理面板中,我可以看到除/wp admin/network/plugins之外的所有页面。php页面和/wp admin/network/upgrade核心。php页面。查看文件夹结构时,这两个文件都存在,但由于某些原因,它们不会加载。非常感谢您的帮助。