简体   繁体   English

Wordpress 如何在插件激活时创建表

[英]Wordpress how to create table on plugin activation

Here is my code, I write create query into function and call that in register_activation_hook() function, but it's not working.这是我的代码,我将创建查询写入 function 并在register_activation_hook() function 中调用它,但它不起作用。

function test_contact_form() {      
        global $wpdb; 
        $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        mysql_select_db(DB_NAME);        
        if (!$connection){ die('Error: ' . mysql_error());}

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

    $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
    if( $wpdb->get_var( "SHOW TABLES LIKE '$db_table_name'" ) != $db_table_name ) {
        if ( ! empty( $wpdb->charset ) )
            $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
        if ( ! empty( $wpdb->collate ) )
            $charset_collate .= " COLLATE $wpdb->collate"; 
        $sql = "CREATE TABLE " . $db_table_name . " (
            id int(11) NOT NULL auto_increment,
                        ip varchar(15) NOT NULL,
                        name varchar(60) NOT NULL,
                        emailid varchar(200) NOT NULL,
                        mobileno varchar(10) NOT NULL,
                        message varchar(1000) NOT NULL,
                        PRIMARY KEY (id) 
        ) $charset_collate;";


dbDelta( $sql ); // create query 
        }  
    }
    register_activation_hook(__FILE__, 'test_contact_form');

May this will help you .. 愿这对您有帮助。

function test_contact_form()
{      
  global $wpdb; 
  $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
  $charset_collate = $wpdb->get_charset_collate();

  $sql = "CREATE TABLE $db_table_name (
                id int(11) NOT NULL auto_increment,
                ip varchar(15) NOT NULL,
                name varchar(60) NOT NULL,
                emailid varchar(200) NOT NULL,
                mobileno varchar(10) NOT NULL,
                message varchar(1000) NOT NULL,
                UNIQUE KEY id (id)
        ) $charset_collate;";

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

register_activation_hook( __FILE__, 'test_contact_form' );

I have modified @SelvaKumar answer. 我已经修改了@SelvaKumar答案。 As in it once plugin gets activated, a $db_table_name table will be created into WordPress. 就像在插件激活后一样,一个$db_table_name表将被创建到WordPress中。 If someone deactivates plugin then activates the plugin this tries to create $db_table_name table again and will raise an error. 如果有人停用了插件,然后又激活了插件,它将尝试再次创建$db_table_name表,并且将引发错误。

In this code, we need to put a check if a table already exists then this script should not more. 在这段代码中,我们需要检查表是否已经存在,然后此脚本不应更多。

function test_contact_form()
{      
  global $wpdb; 
  $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
  $charset_collate = $wpdb->get_charset_collate();

 //Check to see if the table exists already, if not, then create it
if($wpdb->get_var( "show tables like '$db_table_name'" ) != $db_table_name ) 
 {
       $sql = "CREATE TABLE $db_table_name (
                id int(11) NOT NULL auto_increment,
                ip varchar(15) NOT NULL,
                name varchar(60) NOT NULL,
                emailid varchar(200) NOT NULL,
                mobileno varchar(10) NOT NULL,
                message varchar(1000) NOT NULL,
                UNIQUE KEY id (id)
        ) $charset_collate;";

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

register_activation_hook( __FILE__, 'test_contact_form' );

I modified some code in Gufran Hasan answer.我修改了Gufran Hasan答案中的一些代码。 I quoted tables fields and initialised $test_db_version var because sometime it will create problem.我引用了表字段并初始化了 $test_db_version var,因为有时它会产生问题。 Also It will not work in theme functions and inside a function hooked to the 'plugins_loaded' or 'init' hooks, refer refer wordpress support此外,它不会在主题功能和挂钩到“plugins_loaded”或“init”挂钩的 function 内部工作,请参阅 wordpress 支持

function test_contact_form()
{      
  global $wpdb; 
  $test_db_version = '1.0.0';
  $db_table_name = $wpdb->prefix . 'contactus_detail';  // table name
  $charset_collate = $wpdb->get_charset_collate();

 //Check to see if the table exists already, if not, then create it
 if($wpdb->get_var( "show tables like '$db_table_name'" ) != $db_table_name ) 
 {
       $sql = "CREATE TABLE `$db_table_name` (
                `id` int(11) NOT NULL auto_increment,
                `ip` varchar(15) NOT NULL,
                `name` varchar(60) NOT NULL,
                `emailid` varchar(200) NOT NULL,
                `mobileno` varchar(10) NOT NULL,
                `message` varchar(1000) NOT NULL,
                UNIQUE KEY id (id)
        ) $charset_collate;";

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

register_activation_hook( __FILE__, 'test_contact_form' );

The hook "register_activation_hook" will be trigger on plugin activation when you hit Activate link in dashboard. 当您点击仪表板中的“激活”链接时,钩子“ register_activation_hook”将在插件激活时触发。

Your code is ok but it will not work in "function.php" file. 您的代码可以,但是不能在“ function.php”文件中使用。 "register_activation_hook" will work when you creating plugin. 创建插件时,“ register_activation_hook”将起作用。

OR 要么

If you want to create table from "function.php" file. 如果要从“ function.php”文件创建表。 You can simply use 'init' action hook. 您可以简单地使用“ init”动作挂钩。 Just Like that 就这样

add_action('init','test_contact_form'); add_action('init','test_contact_form');

You simply replace your last line with above one 您只需将以上一行替换为最后一行

you have written this lines , 你已经写了这行,

 $connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        mysql_select_db(DB_NAME);        
        if (!$connection){ die('Error: ' . mysql_error());}

that's not needed. 不需要。 And you don't need to check for $wpdb variables in if. 而且您不需要在if中检查$wpdb变量。

Look at below linked question and their answer for your question's solution. 请查看以下链接的问题及其答案,以解决您的问题。 https://stackoverflow.com/a/29890105/2219158 https://stackoverflow.com/a/29890105/2219158

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM