I’m running two WordPress installations that need to share the same content database. I want both sites to use identical article and metadata tables.
Right now I’ve set up custom table definitions in my second site’s configuration:
define( 'SHARED_ARTICLES_TABLE', 'mainsite_posts' );
define( 'SHARED_METADATA_TABLE', 'mainsite_postmeta' );
I also modified the database class in wp-includes/wp-db.php around line 1050:
if ( isset( $db_tables['posts'] ) && defined( 'SHARED_ARTICLES_TABLE' ) )
$db_tables['posts'] = SHARED_ARTICLES_TABLE;
if ( isset( $db_tables['postmeta'] ) && defined( 'SHARED_METADATA_TABLE' ) )
$db_tables['postmeta'] = SHARED_METADATA_TABLE;
This works great and both sites now display the same content. However, when WordPress updates, my core file changes get wiped out.
I tried creating a custom function in my theme’s functions.php to replace the core functionality:
add_filter( 'database_tables', 'custom_table_override', 15, 2 );
function custom_table_override( $table_scope = 'all', $use_prefix = true, $site_id = 0 ) {
if ( isset( $db_tables['posts'] ) && defined( 'SHARED_ARTICLES_TABLE' ) )
$db_tables['posts'] = SHARED_ARTICLES_TABLE;
if ( isset( $db_tables['postmeta'] ) && defined( 'SHARED_METADATA_TABLE' ) )
$db_tables['postmeta'] = SHARED_METADATA_TABLE;
return $db_tables;
}
But this crashes my site with a 500 error. What’s the proper way to override WordPress core database functions so my changes survive updates?