Created
January 27, 2012 10:48
-
-
Save timwhitlock/1688245 to your computer and use it in GitHub Desktop.
What require_wp_db function in Wordpress would look like, if it worked
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function require_wp_db() { | |
global $wpdb; | |
if ( isset($wpdb) ){ | |
return; | |
} | |
// Should check for override db.php file first | |
// - including both means fatal error on duplicate class | |
if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ){ | |
require_once( WP_CONTENT_DIR . '/db.php' ); | |
} | |
// include core default | |
else { | |
require_once( ABSPATH . WPINC . '/wp-db.php' ); | |
} | |
// The db.php adapter may have defined the global for us | |
if( ! isset($wpdb) ){ | |
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); | |
} | |
} |
This patch isn't necessary. Your db.php
file shouldn't define the wpdb
class, it should define a class that extends it and then instantiate it (unless you want to write a completely new db driver class from scratch, then you don't need to extend wpdb
).
Example db.php:
class my_db_driver extends wpdb {
public function some_method_i_want_to_override() {
// Hello!
}
}
$wpdb = new my_db_driver( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
Sorry, got tripped up by the comment editor there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also patch wp-admin/install.php to call this function rather than the hard-coded file inclusion around line 42