2

For some reason my activation hook is not creating new database table.

Code:

function mm_booking_activate() {
    global $wpdb;
    $mm_booking_table_name = $wpdb->prefix . "mm_booking";
    $mm_booking_db_version = '1.0';
    $charset_collate = $wpdb->get_charset_collate();

    if ( $wpdb->get_var( 'SHOW TABLES LIKE ' . $mm_booking_table_name ) != $mm_booking_table_name ) {
        $sql = "CREATE TABLE $mm_booking_table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            first_name varchar(255),
            last_name varchar(255),
            address varchar(255),
            lat decimal(10, 8) NOT NULL,
            lng decimal(11, 8) NOT NULL,
            field enum('option1', 'option2', 'option3'),
            phone_number varchar(255),
            email varchar(255),
            date_time_from datetime DEFAULT CURRENT_TIMESTAMP,
            date_time_to datetime DEFAULT CURRENT_TIMESTAMP,
      status enum('pending', 'approved', 'rejected') DEFAULT 'pending',
      modification_time datetime ON UPDATE CURRENT_TIMESTAMP,
      creation_time datetime DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY  (id)
    ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        add_option( 'mm_booking_db_version', $mm_booking_db_version );
    }
}
register_activation_hook( __FILE__, 'mm_booking_activate' );
  • Do you have debugging enabled? Any error messages? Have you verified that your function fires on activation? – Andy Macaulay-Brook Oct 7 '16 at 18:45
2

The table name needs to be in quotes on the line where you're checking if it exists. I tested it, and after that was fixed, the table was successfully created. Here is the full code:

function mm_booking_activate() {
    global $wpdb;
    $mm_booking_table_name = $wpdb->prefix . "mm_booking";
    $mm_booking_db_version = '1.0';
    $charset_collate = $wpdb->get_charset_collate();

    if ( $wpdb->get_var( "SHOW TABLES LIKE '{$mm_booking_table_name}'" ) != $mm_booking_table_name ) {
        $sql = "CREATE TABLE $mm_booking_table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            first_name varchar(255),
            last_name varchar(255),
            address varchar(255),
            lat decimal(10, 8) NOT NULL,
            lng decimal(11, 8) NOT NULL,
            field enum('option1', 'option2', 'option3'),
            phone_number varchar(255),
            email varchar(255),
            date_time_from datetime DEFAULT CURRENT_TIMESTAMP,
            date_time_to datetime DEFAULT CURRENT_TIMESTAMP,
      status enum('pending', 'approved', 'rejected') DEFAULT 'pending',
      modification_time datetime ON UPDATE CURRENT_TIMESTAMP,
      creation_time datetime DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY  (id)
    ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        add_option( 'mm_booking_db_version', $mm_booking_db_version );
    }
}
register_activation_hook( __FILE__, 'mm_booking_activate' );
  • 1
    Thank you that kind of worked. But my problem was MaraiaDB which doesn't support multiple DEFAULT CURRENT_TIMESTAMP and few other things. If anyone else has this problem, I suggest running it first in phpMyAdmin and when it works just copy/paste in code. Thanks. – Milos Nov 10 '16 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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