Create table with dbDelta,can't put any DEFAULT data

I want to add more custom tables to wp_users.
but I want them to contain a default JSON data.
This one works...when the DEFAULT is empty...

    register_activation_hook(__FILE__, 'toto_user_db_setup');
function toto_user_db_setup() {
    global $wpdb;
    $user_data = "wp_users";

    $charset_collate = $wpdb-get_charset_collate();

    $game_1 = "CREATE TABLE $user_data (
        data1 tinytext DEFAULT '' NOT NULL,
        data2 tinytext DEFAULT '' NOT NULL,
        data3 mediumtext DEFAULT '' NOT NULL,
        data4 mediumtext DEFAULT '' NOT NULL,
        data5 longtext DEFAULT '' NOT NULL      
    ) $charset_collate;";

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

}

But when i put inside DEFAULT any value... it does not work at all.

    register_activation_hook(__FILE__, 'toto_user_db_setup');
function toto_user_db_setup() {
    global $wpdb;
    $user_data = "wp_users";

    $charset_collate = $wpdb-get_charset_collate();

    $game_1 = "CREATE TABLE $user_data (
        data1 tinytext DEFAULT 'test1' NOT NULL,
        data2 tinytext DEFAULT 'test2' NOT NULL,
        data3 mediumtext DEFAULT 'hello' NOT NULL,
        data4 mediumtext DEFAULT '2132' NOT NULL,
        data5 longtext DEFAULT 'dada' NOT NULL      
    ) $charset_collate;";

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

}

I did try to make it work hours.....but nothing.... please help.

Topic dbdelta database Wordpress

Category Web


  1. It appears as though you're altering the users table - this is a WordPress core table (if using the wp_ prefix) and this is probably not a good idea - create a separate table with an ID foreign key to avoid potential problems.
  2. Use $wpdb->prefix to get the prefix that's actually being used on a given WordPress installation (you can't depend upon it being wp_, it's configurable)
  3. dbDelta() is very particular but it should accept a default value; assign the results of dbDelta() to a variable to see what's happening:

    $result = dbDelta($game_1);
    var_dump( $result );
    

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.