0

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.

2
  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 );
    
| improve this answer | |
  • For some reason.... after i done $result = dbDelta($game_1); var_dump( $result );... i can't login anymore – Kefka Sep 20 '18 at 2:33
  • how do I get all users id to another database table? – Kefka Sep 20 '18 at 3:25
  • @Kefka RE: logging in - this is probably because your script is outputting information (with var_dump()) before it can send headers - just remove the var_dump() call (and work on a local development instance if at all possible); RE: user ID's - this is a topic for a separate question. – Gray Sep 20 '18 at 3:55

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.