What is the correct way to use WordPress functions outside WordPress files?

There’s little difference between the files. When you view a WordPress page, the first file called is index.php. And it is, essentially, your “Method 1:”

define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require ('./wp-blog-header.php');

The blog header file (that queues up the rest of WordPress) loads wp-load.php directly and fires up WordPress itself. Here’s most of wp-blog-header.php:

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    require_once( dirname(__FILE__) . '/wp-load.php' );

    wp();

    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

So the difference between your two methods is … what’s loaded.

Method 1 is exactly what WordPress does to load itself (with the exception of turning themes off). So if you need all of WordPress and want to fire all of the default hooks/actions, go with that route.

Method 2 is just a further step down the line. It loads all of WordPress, but doesn’t call wp() or invoke the template loader (used by themes). Method 2 will be a little lighter-weight, but should give you the same functionality.

Leave a Comment