WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to access a second database with my wordpress system. To do this, I've attempted to follow the advice found here which seems to have worked for a lot of people.

In summary, I'm adding the following line to my functions.php:

global $newdb;
$newdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

Unfortunately, when I add this I come up with the following error:

Fatal error: Class 'wpdb' not found in /home/mine/www/wp-includes/functions.php

I'm sure I must be missing wrong (obviously, right?) but the instructions don't seem to have any extra steps that I've been leaving out.

Thanks for any help

share|improve this question
did you make it global ? ..... global newdb ? – pl4g4 Oct 4 '13 at 19:03
@pl4g4 Thanks for the response! I did, and i'll add that to my description above.... I assume you're making sure that I had "global $newdb" preceding the rest of my $newdb code, right? – neanderslob Oct 4 '13 at 19:12
yes, Remember that you have to declare $newdb to be a global variable in the function that you’re going to use it. – pl4g4 Oct 4 '13 at 19:17
Are your constants (DB_USER, DB_PASSWORD, etc) all defined somewhere? Are you calling new wpdb() in a filter or action, or just bare in the functions.php file? – Pat J Oct 4 '13 at 19:18
@PatJ DB_USER and alike have their attributes hard-coded in (I simply used generic terms for interweb privacy purposes). The calling of new wpdb() is just bare in functions.php. Might that be an issue? Thanks for the reply! – neanderslob Oct 4 '13 at 19:25
up vote 3 down vote accepted

When enhancing WordPress functionality, always use the theme's functions.php file (located, generally, in {WP root}/wp-content/themes/{your theme}/functions.php), or put your code into a plugin.

The rules for hacking core code are similar to the rules for optimization.

  1. Don't do it.
  2. (for experts only) Don't do it yet.
share|improve this answer

try something like this

add_action('init', 'doSomething');
function doSomething(){
     global $newdb;
     $newdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
share|improve this answer
Alright, this is getting weird. Now it's giving me Fatal error: Call to undefined function add_action() That shouldn't be a problem, should it? – neanderslob Oct 4 '13 at 19:32
that code is working for me. where r u adding the code? ... check what @Pat J said. – pl4g4 Oct 4 '13 at 19:40
yea..... you were on the right track with that question. PatJ above rightly chided me for editing my wp-includes file and pointed me in the right direction. Thanks for trying to help me bumble my way through this though! – neanderslob Oct 4 '13 at 19:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.