How to use hyperdb to separate and share a user dataset between wordpress installs?

I believe Crazycoolcam spelled the table incorrectly, user_meta vs usermeta. You may also want to define the else case, what happens if the table is not user.

Try this:

<?php 
        $wpdb->add_database(array( //Connect to Users Database
        'host'     => DB_HOST, // I am using the same host for my two DBs
        'user'     => DB_USER, // I am using the same username for my two DBs
        'password' => DB_PASSWORD, // I am using the same p/w for my two DBs
        'name'     => 'my_user_db_name', 
        'write'    => 0, // Change to 1 if you want your slave site's the power to update user data.
        'read'     => 1,
        'dataset'  => 'user'
    ));

    $wpdb->add_database(array( // Main Database
        'host'     => DB_HOST,
        'user'     => DB_USER,
        'password' => DB_PASSWORD,
        'name'     => DB_NAME,
        'write'    => 1, // Change to 1 if you want your slave site's the power to update user data.
        'read'     => 1,
        'dataset' => 'global'
    ));

    $wpdb->add_callback('user_callback');

    function user_callback($query, $wpdb) {
        if ( $wpdb->base_prefix . 'users' == $wpdb->table || $wpdb->base_prefix . 'usermeta' == $wpdb->table) {
            return 'user'; 
        } else {
            return 'global';
        }
    }

Leave a Comment