Setup 3 Sites To Connect To 1 Database and Share Data

To elaborate on my suggestion, here is some working code:

// hook in before theme is loaded
add_action('plugins_loaded','custom_maybe_switch_themes');
function custom_maybe_switch_themes() {
    if (!is_user_logged_in()) {return;}
    if (!current_user_can('manage_options')) {return;}

    // check for query request eg. ?usertheme=theme-slug
    if (isset($_REQUEST['usertheme'])) {
        // sanitize request
        $usertheme = strtolower(sanitize_title($_REQUEST['usertheme']));
        global $current_user; $current_user = wp_get_current_user();

        // maybe reset user meta stylesheet
        if ($usertheme == 'reset') {delete_user_meta($current_user->ID,'stylesheet'); return;}

        // validate theme
        $theme = wp_get_theme($usertheme);
        if ($theme->exists()) {
             delete_user_meta($current_user->ID,'stylesheet');
             add_user_meta($current_user->ID,'stylesheet',$usertheme);
        }
    }
}

// add filter to stylesheet option
add_filter('pre_option_stylesheet', 'get_user_stylesheet');
function get_user_stylesheet($stylesheet) {
    if (!is_user_logged_in()) {return $stylesheet;}
    if (!current_user_can('manage_options')) {return $stylesheet;}

    // check user meta stylesheet
    global $current_user; $current_user = wp_get_current_user();
    $userstylesheet = get_user_meta($current_user->ID,'stylesheet',true);
    // if we have a value use it instead
    if ($userstylesheet) {return $userstylesheet;}
    return $stylesheet;
}

To switch to a theme on a user basis, use ?usertheme=theme-slug

…and to reset just use ?usertheme=reset

Since all the theme code is loaded dependant on the stylesheet used, you could set up Child Themes directories for Live, Staging and Development (with the same Parent Template) and switch between themes really easily as a developer and see the results immediately on the same domain. 🙂

The only caveat I can think of is testing major plugin updates, you would still need a development environment for that purpose and maybe sync the database across to test them with the current database content. This is only because you don’t know if they will change their options database table structure (though rare, there is no going back so you could break a live site that way.)

Something further would be needed to test the site output for logged out users, but that could be done with a different querystring for one-off loads, or with cookies instead of user meta for a similar persistent result.

Anyway, the advantage of being able to have code snippets organized in files for testing in development and staging on the same domain, making pushing new changes between them really easy, and to not have to worry about database syncing all the time (though that’s another workable approach) makes this a very convenient way of doing things I think.

error code: 523