Skip to content

Instantly share code, notes, and snippets.

@timwhitlock
Created January 27, 2012 10:48
Show Gist options
  • Select an option

  • Save timwhitlock/1688245 to your computer and use it in GitHub Desktop.

Select an option

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
<?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 );
}
}
@timwhitlock
Copy link
Copy Markdown
Author

Also patch wp-admin/install.php to call this function rather than the hard-coded file inclusion around line 42

@johnbillion
Copy link
Copy Markdown

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 );

@johnbillion
Copy link
Copy Markdown

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