1
votes

I want to insert a table into WordPress database while plugin activated. I have used this code but it is not working. This code should create a table while plugin gets activated, but in my WordPress when I activate the plugin, the code works, but table is not being created in the database.

How can I create the table in WordPress database while plugin activated?

I have use the below code for my plugin

class createtabel {
    function __construct() {
        register_activation_hook( __FILE__, 'create_plugin_database_table' );
    }

    public static function create_plugin_database_table() {
        global $wpdb;
        $table_name = $wpdb->prefix . 'wp_productqa';
        $sql = "CREATE TABLE $table_name (
        QA_id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
        Product_ID varchar(50) NOT NULL,
        Username varchar(50)NOT NULL,
        Useremail varchar(50) NOT NULL,
        comment varchar(255) NOT NULL,
        PRIMARY KEY  (QA_id)
        );";
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
    }
}
2
how you can call this class function like that - Md Hasibur Rahaman
class ProductQA { function createdatabase() { $classdb= new createtabel(); register_activation_hook( FILE, array( $this) ); } } $productQAObject->createdatabase(); - R_Solanki
$createTable = new createtabel; register_activation_hook( FILE, array($createTable, create_plugin_database_table)); - barudo
but I want to use from another page one is my productqa.php page and other is classdb in productqa page include classdb and call classdb object - R_Solanki
Use WordPress built in $wpdb class for database calls: codex.wordpress.org/Class_Reference/wpdb - dingo_d

2 Answers

0
votes

You have to make sure register_activation_hook is called.

For instance, in your plugin definition file you may:

$createTable = new createtabel;
register_activation_hook(__FILE__, array($createTable, 'create_plugin_database_table'));

BTW, in the createtabel class you can create your table with:

$wpdb->query($sql);

Is that your intention also to have

$table_name = $wpdb->prefix . 'wp_productqa';

which in most likelihood (assuming prefix is wp_)will have:

$table_name = 'wp_wp_productqa';
0
votes

Please use this code while activate your plugin it will create new table in your database.

register_activation_hook(__FILE__, 'install_covercarousel');

Define this above hook in globally where your file coding has been started.

Also please dont' use public static function just add function and functioname like this function abs(){}

After changed code look like this

register_activation_hook( __FILE__, 'create_plugin_database_table' );
function create_plugin_database_table() {
            global $wpdb;
            $table_name = $wpdb->prefix . 'wp_productqa';
            $sql = "CREATE TABLE $table_name (
            QA_id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
            Product_ID varchar(50) NOT NULL,
            Username varchar(50)NOT NULL,
            Useremail varchar(50) NOT NULL,
            comment varchar(255) NOT NULL,
            PRIMARY KEY  (QA_id)
            );";
            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            dbDelta( $sql );
        }