[wp-hackers] Bootstrap WordPress
Ryan McCue
lists at rotorised.com
Fri Apr 13 01:53:01 UTC 2012
Ryan McCue wrote:
> This does come with problems if you're loading it inside a function, as
> globals are set inside this without being explicitly declared as globals.
Just to expand on this a little further, here's a walkthrough of how one
piece of software we have links in with WordPress.
Each function/method that needs to use any WordPress feature calls
load_wp(). load_wp() looks like this:
public static function load_wp() {
static $is_loaded = false;
if ($is_loaded) {
return;
}
if (empty(self::$wp_path)) {
throw new Exception('WordPress could not be loaded', 500);
}
global $wp_the_query, $wp_query, $wp_rewrite, $wp;
define('DISABLE_WP_CRON', true);
require_once(self::$wp_path . '/wp-load.php');
$is_loaded = true;
}
What this does is ensure that:
a) WordPress is only loaded when needed (since this software
incorporates other pieces of data from various systems)
b) WordPress is only loaded once regardless of how many times load_wp()
is called
c) Globals are set up correctly (although this does miss some. A better
solution would be an array_diff on get_defined_vars() before/after
loading, and then globalising these. An even better solution would be if
WordPress explicitly declared them as global to start with.
d) Cron is not loaded (this is for an API, so we want to avoid excess
queries and work where possible)
Hopefully this helps. :)
(If this seems badly written, or slightly insane, it's because I'm
slightly jetlagged, so apologies in advance.)
--
Ryan McCue
<http://ryanmccue.info/>
More information about the wp-hackers
mailing list