0

I am trying to create some custom database table on wordpress plugin activation with php OOP concept. But no database is created after plugin activation. Here is my code

class wpe_Main {
    public function __construct() {
        register_activation_hook( __FILE__, array($this,'create_wpe_enquiry_table'));
    }

    public function create_wpe_enquiry_table(){
        global $wpdb;
        $sql_enquiry = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpe_enquiry (
        `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        `userid` int(11) NOT NULL,
        `enquiry_type_id` varchar(3) NOT NULL,
        `contact_email` varchar(200) NOT NULL,
        `contact_number` varchar(50) NOT NULL,
        `contact_messenger` varchar(200) NOT NULL,
        `query_title` varchar(200) NOT NULL,
        `query_content` text NOT NULL,
        `attachment1` varchar(200) NOT NULL,
        `attachment2` varchar(200) NOT NULL,
        `attachment3` varchar(200) NOT NULL,
        `user_ip` varchar(50) NOT NULL,
        `query_cdate`  DATETIME DEFAULT CURRENT_TIMESTAMP,
        `query_udate` DATETIME ON UPDATE CURRENT_TIMESTAMP,
        `status` varchar(2) NOT NULL
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8";

        $sql_enquiry_reply = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpe_query_reply (
        `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        `quiry_id` int(11) NOT NULL,
        `reply_content` text NOT NULL,
        `user_ip` varchar(50) NOT NULL,
        `reply_cdate`  DATETIME DEFAULT CURRENT_TIMESTAMP,
        `reply_udate` DATETIME ON UPDATE CURRENT_TIMESTAMP,
        `status` varchar(2) NOT NULL
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8";

        $sql_enquiry_type = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpe_query_part (
        `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        `part_value` int(11) NOT NULL,
        `part_name` varchar(200) NOT NULL,
        `user_ip` varchar(50) NOT NULL,
        `reply_cdate`  DATETIME DEFAULT CURRENT_TIMESTAMP
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql_enquiry );
    dbDelta( $sql_enquiry_reply );
    dbDelta( $sql_enquiry_type );
}
}
$wpuf = new wpe_Main();

2 Answers 2

1

Is your class wpe_Main file is the mail file of the plugin?

Because __FILE__ should return main plugin file path. So if your wpe_Main class is in a sub directory then the register activation won't work.

If you want you can try like below to avoid __FILE__ confusion

Plugin path: wp-content/plugin/sampleplugin/sample.php

Then the hook should be like below

register_activation_hook( 'sampleplugin/sample.php', 
array($this,'create_wpe_enquiry_table'));

Updated after comments

If Your class in main plugin file then for sure the function is executing on plugin activation. Your table are not creating might be SQL error or typos. So please check the function responsible for creating table.

For simple testing on activation function try the below code

update_option('activation_test','its working');

then check the options, you can see that it should been updated or you can even send mail to test.

Testing the activation function is your conviniens. At the end register_activation_hook will work on OOPS without any problem. Check Reference

4
  • wpe_Main class is in main path of the plugins. After removing OOP class this code is ok but problem creates under the OOP class concept. Dec 30, 2015 at 15:14
  • then it is working for sure. check my updated answer
    – WPTC-Troop
    Dec 30, 2015 at 15:45
  • Problem was on database query. Now it is solved. Thanks Dec 30, 2015 at 17:59
  • Super. This is what I suggested. Thumbs up
    – WPTC-Troop
    Dec 31, 2015 at 2:11
2

You're not using the real powers of dbDelta() here, when you use the IF NOT EXISTS clause.

With the IF NOT EXISTS clause

When you use the IF NOT EXISTS check in:

CREATE TABLE IF NOT EXISTS

then dbDelta() will do the following matches:

if ( preg_match( "|CREATE TABLE ([^ ]*)|", $qry, $matches ) ) {
    $cqueries[ trim( $matches[1], '`' ) ] = $qry;

Here $matches is:

Array
(
    [0] => CREATE TABLE IF
    [1] => IF
)

and

trim( $matches[1], '`' )

gives the table name IF.

Then it will try to find it's fields with:

$tablefields = $wpdb->get_results("DESCRIBE {$table};");

or DESCRIBE IF that will (hopefully) not give any results. Thus dbDelta() will only try to run the raw creation query but skip any possible future updates to the table structure.

Without the IF NOT EXISTS clause

If you remove the IF NOT EXISTS part, then the first runtime, for e.g. the wp_wpe_query_part table, will be

CREATE TABLE IF NOT EXISTS wp_wpe_query_part (
        `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
        `part_value` int(11) NOT NULL,
        `part_name` varchar(200) NOT NULL,
        `user_ip` varchar(50) NOT NULL,
        `reply_cdate`  DATETIME DEFAULT CURRENT_TIMESTAMP
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8

and for the second runtime, when the table already exists, we will get these queries:

DESCRIBE wp_wpe_query_part;

SHOW INDEX FROM wp_wpe_query_part;

ALTER TABLE wp_wpe_query_partCHANGE COLUMN id `id` int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY;

ALTER TABLE wp_wpe_query_partCHANGE COLUMN part_value `part_value` int(11) NOT NULL;

ALTER TABLE wp_wpe_query_partCHANGE COLUMN part_name `part_name` varchar(200) NOT NULL;

ALTER TABLE wp_wpe_query_partCHANGE COLUMN user_ip `user_ip` varchar(50) NOT NULL;

ALTER TABLE wp_wpe_query_partCHANGE COLUMN reply_cdate `reply_cdate`  DATETIME DEFAULT CURRENT_TIMESTAMP;

If we later make some changes to the table structure, it will then modify the current table accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.