Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a plugin for a system to use Wordpress as the authentication backend, so that I can use my existing Wordpress users in another application. The plugin works great when running in single website mode:

include /path/to/wordpress/wp-config.php
global $wpdb;
// SQL and code to select user and compare entered passwords

I need this to work against a Wordpress Multisite install as well. When I pull in the wp-config.php for the MS site, I end up with this error:

Fatal error: Call to a member function set_prefix() on a non-object in /path/to/wordpress/wp-includes/ms-settings.php on line 126

On line 126, WP is trying to set the table prefix against $wpdb, but for some reason $wpdb doesn't exist here. I ran xdebug and $wpdb does get created, but ms-settings.php doesn't see it. I can fix this by adding:

global $wpdb;

right before line 126 and it works, but I don't want to modify the core Wordpress code. Is there a better way to bootstrap Wordpress ?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You'll want to include the wp-load.pphp, not the wp-config.php.

Depending on how you're doing it, you may also have to set $_SERVER variables if they aren't already set to prevent WordPress from trying to redirect you. For example:

$_SERVER = array(
  "HTTP_HOST" => "http://mysite.com",
  "SERVER_NAME" => "http://mysite.com",
  "REQUEST_URI" => "/",
  "REQUEST_METHOD" => "GET"
);
share|improve this answer
add comment

I think what you are experiencing is that you are trying to load Wordpress from inside a method, function or call back that doesn't have access to the global scope.

If Wordpress is loaded outside of this - in my case ran with no errors.

share|improve this answer
add comment

Your Answer

 
discard

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.