Using the WordPress database outside WordPress in version 3.4+

My old workaround for using $wpdb no longer works in WordPress 3.4, but there is similar functionality built-in now. It loads a lot more than I was in my old method, but not as much as the full WordPress core being loaded.

WordPress 3.4+ Method

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

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.


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

Are your subdomains in order?

I was recently trying to recall a subdomain (for example: I had set up for a client, and tried guessing at what I thought it was. While I guessed wrong, it highlighted an important issue to address.

Wildcard DNS

The subdomain I tried was not an actual site for the client, but a wildcard DNS entry was sending it to the IP of the server anyways. If you have a wildcard entry set up for your domain(s) you should take care to see what your web server is serving up for subdomains you aren’t actually using. In most cases, it will serve up the default site for that IP address, but under whatever URL you used. Read the rest of this entry »

Share This on Facebook

No doubt you’ve seen the nifty Share this on Facebook links on websites, maybe you even have them on your site already.  Did you know that you can also craft titles, descriptions, and a thumbnail for the page to pre-populate in the sharing box?  Follow the instructions after the break to get share this links on your site if you don’t already have them, and below that you’ll find some tips on getting titles, descriptions, and thumbnails added.
Read the rest of this entry »