WP 3.1 – pg4wp – Cannot redeclare class wpdb
-
Hi,
My blog was ok whith WordPress 3.0.4 and pg4wp 1.1.0 until I updated to wordpress 3.1 !
PHP logs shows a PHP Fatal error: Cannot redeclare class wpdb in /usr/share/wordpress/htdocs/wp-content/db.php(37) : eval()'d code on line 52
In wordpress 3.0.5 the require_wp_db(); – call in the /wp-settings.php – is implemented in the file /wp-includes/functions.php which is :
function require_wp_db() { global $wpdb; if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ) require_once( WP_CONTENT_DIR . '/db.php' ); else require_once( ABSPATH . WPINC . '/wp-db.php' ); }
The class wpdb – in /wp-includes/wp-db.php – is evaluated by the eval function in /wp-content/db.php file from pg4wp :
eval( str_replace( array_keys($replaces), array_values($replaces), file_get_contents(ABSPATH.'/wp-includes/wp-db.php')));
The evaluation is done after the replacement of MySQL names to postgres ones.
But in WordPress 3.1, require_wp_db() function has moved into the file /wp-includes/load.php. The new implementation is :
function require_wp_db() { global $wpdb; require_once( ABSPATH . WPINC . '/wp-db.php' ); if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ) require_once( WP_CONTENT_DIR . '/db.php' ); if ( isset( $wpdb ) ) return; $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); }
in those conditions; /wp-includes/wp-db.php is evaluated twice ! with the wp-content/db.php. The class wpdb can not be redeclared twice.
My solution is to … come back. I simply re-implemented the WP 3.1 require_wp_db() function like this :
function require_wp_db() { global $wpdb; if ( file_exists( WP_CONTENT_DIR . '/db.php' ) ) require_once( WP_CONTENT_DIR . '/db.php' ); else require_once( ABSPATH . WPINC . '/wp-db.php' ); if ( isset( $wpdb ) ) return; $wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ); }
Hope, this could help …
- The topic ‘WP 3.1 – pg4wp – Cannot redeclare class wpdb’ is closed to new replies.