优秀的解决方案是:
my_wpdb(\'test\',\'pass\', \'database\', \'host\', \'../path/to/wp/\');
功能
function my_wpdb($user, $pass, $db, $host, $path_to_root= \'/../../../../../../\', $run_wp_config=true){
$path_to_wp= dirname(__DIR__) .$path_to_root;
//execute wp-config, if not run already
if($exec_config && !defined(\'DB_HOST\')){ preg_match(\'/\\<\\?php(.*?)\\/\\* That\\\'s all, stop editing/si\', file_get_contents($path_to_root."wp-config.php"), $found); eval($found[1]); }
require_once( $path_to_wp.\'wp-includes/wp-db.php\' ); if ( file_exists($path_to_wp.\'wp-content/db.php\' ) ) require_once($path_to_wp.\'wp-content/db.php\' );
return $GLOBALS[\'wpdb\'] = new wpdb( $user, $pass, $db, $host );
}