WordPress插件样板表格创建

时间:2016-06-18 作者:bradley546994

嗨,我有个可怕的问题。我在页面上生成了代码插件:http://wppb.me/

现在,我决不能向SQL数据库安装中添加表。我试着这样做:包括/分类我的激活器。php

class My_Activator {
    public static function activate() {

        register_activation_hook( __FILE__,\'jal_install\' );
    }


    function jal_install()
    {

        global $wpdb;
        global $jal_db_version;
        $table_name = $wpdb->prefix . \'fnotice\';
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
                    id INT NOT NULL AUTO_INCREMENT,
                    msg varchar(500) NOT NULL,
                    time DATETIME NOT NULL,
                    type varchar(350) NOT NULL,
                    link varchar(350) NOT NULL,
                    status ENUM(\'0\', \'1\') NOT NULL default \'1\',
                    PRIMARY KEY (id) )";

        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );

        add_option( \'jal_db_version\', $jal_db_version );

    }

}

1 个回复
SO网友:Hamed Moodi

你最好这样写:

class My_Activator {

    public function jal_install()
    {

        global $wpdb;
        global $jal_db_version;
        $table_name = $wpdb->prefix . \'fnotice\';
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
                    id INT NOT NULL AUTO_INCREMENT,
                    msg varchar(500) NOT NULL,
                    time DATETIME NOT NULL,
                    type varchar(350) NOT NULL,
                    link varchar(350) NOT NULL,
                    status ENUM(\'0\', \'1\') NOT NULL default \'1\',
                    PRIMARY KEY (id) )";

        require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
        dbDelta( $sql );

        add_option( \'jal_db_version\', $jal_db_version );

    }

}

$myActivator = new My_Activator();
register_activation_hook( __FILE__, array( $myActivator, \'jal_install\' ) );