Skip to content

Instantly share code, notes, and snippets.

@mustafauysal
Created February 24, 2019 13:47
Show Gist options
  • Save mustafauysal/458fa5e7abba65a5ef29923faf2ed7a0 to your computer and use it in GitHub Desktop.
Save mustafauysal/458fa5e7abba65a5ef29923faf2ed7a0 to your computer and use it in GitHub Desktop.
HyperDB multisite example
<?php
$wpdb->add_database( array(
'host' => 'global.db.example.com',
'user' => 'globaluser',
'password' => 'globalpassword',
'name' => 'globaldb',
) );
$wpdb->add_database( array(
'host' => 'blog.db.example.com',
'user' => 'bloguser',
'password' => 'blogpassword',
'name' => 'blogdb',
'dataset' => 'blog',
) );
$wpdb->add_callback( 'my_db_callback' );
// Multisite blog tables are "{$base_prefix}{$blog_id}_*"
function my_db_callback( $query, $wpdb ) {
if ( preg_match("/^{$wpdb->base_prefix}\d+_/i", $wpdb->table) ) {
return 'blog';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment