Using wpdb, the WordPress database, outside of WordPress

Occasionally it may be useful to have a dynamic page outside of WordPress that can still access the $wpdb object. If you search around the internet, you’ll eventually find that loading the wp-load script (example below) will make the database object available to you.

Old Way

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

One problem with that solution is that it loads ALL the overhead of WordPress, which may be what you want if you need to use “The Loop” in your page, but usually isn’t what I’m looking for. If you’re using a caching plugin like W3TC to speed up your site, you’ll notice that including wp-load triggers W3TC to take over your page, which can give unexpected results. To work around this, I now set up the database using the few lines below.

New Way

define( 'BLOCK_LOAD', true );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-includes/wp-db.php' );
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

This does require one small change to the wp-config file to add support for blocking it from loading the rest of WordPress. You’ll need to find the line in the file that includes wp-settings and change it to look like the line below. In normal WordPress usage it will continue to include the wp-settings file as before, but in our special case, it will stop and only load the database settings.

if ( !( defined('BLOCK_LOAD') && BLOCK_LOAD ) ) require_once(ABSPATH . 'wp-settings.php');

NOTE: You may have to edit the paths above to reference the proper location of your WordPress install.

UPDATE

WordPress 3.4 broke this method, but I have a new method.

Share Your Thoughts

Hi, is and , which you swear not to share with anyone, is .
If you're interested, I also have you can read at (optional).
Below are for this post:

11 Responses to “Using wpdb, the WordPress database, outside of WordPress”

  1. Braunson says:

    Very handy, thanks!

  2. Tim B. says:

    Thanks! This was exactly what I needed to set up a cron job using an independent php file with WordPress functions! You rock!

  3. Chiang Mai Expat says:

    Hi, exactly what I was looking for.

    I am trying to write a small WordPress widget that will display the most “facebook liked” posts. I was wondering how to add a link in the backoffice of the widget to manually refresh the counts (the results will obviously be cached).

    With your method, an ajax call to an external script that will retrieve all the posts and get their facebook likes will do the trick.

    Thanks a lot, this helps me a lot !

  4. Lee says:

    Thanks for this useful post.

    I have a separate CodeIgniter App and want it to create a user on a WP site on a separate domain which I would ideally want on a separate server. Do you know if this is possible or will they have to be on the same box?

    • Andrew says:

      I’m sure you could make this work, but you will probably have to be careful when specifying your database server in the WordPress configuration, setting it to localhost obviously wouldn’t work on both servers.

  5. Seth Caldwell says:

    this doesn’t seem to be working for me; I’m getting fatal errors when trying to load a page that uses wpdb.
    Inside the wpdb class I notice calls to “$db->” but where is this db variable ever created? I can’t find it.

  6. Seth Caldwell says:

    inside wp-db.php I had to
    require_once(“plugin.php”);
    because it was using some apply_filter function that was defined in there.

  7. george says:

    Unfortunately… using latest release, the class is dependent upon plugin.php… and of all things, load.php where a stinking function exists that causes nothing but problems.

    Great idea though.

  8. Jackson says:

    Or…

    define(‘WP_USE_THEMES’, false);
    require(‘./wp-blog-header.php’);

    http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    • Andrew says:

      Yes, you could do that, but my method loads 100x faster based on my unscientific testing (0.4 seconds vs 0.004 seconds on one of my production servers).