Skip to content

Instantly share code, notes, and snippets.

@dave1010
Created April 26, 2011 16:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave1010/942539 to your computer and use it in GitHub Desktop.
Save dave1010/942539 to your computer and use it in GitHub Desktop.
Load WordPress from a CLI not in the global scope
<?php
// When loading WP from phpunit, phpunit doesn't run WP in the global scope.
// This breaks WP. This is an attempt to fix it.
function de_globalize_wp() {
// pretend this is apache
// I use $_SERVER['HTTP_HOST'] or a file called "host" to get the right DB settings
$_SERVER['HTTP_HOST'] = trim(file_get_contents(__DIR__ . '/host'));
$_SERVER["REQUEST_METHOD"] = 'GET';
$_SERVER["SERVER_PROTOCOL"] = 'HTTP/1.0';
$_SERVER["SERVER_PORT"] = '80';
$_SERVER["SERVER_NAME"] = $_SERVER["HTTP_HOST"];
$_SERVER["REMOTE_ADDR"] = 'localhost';
$_SERVER["REMOTE_PORT"] = '80';
$GLOBALS[ '_wp_deprecated_widgets_callbacks' ] = array();
// WP assumes it's being loaded globally
global $wp_version;
global $PHP_SELF;
global $wp_embed;
global $wpdb;
global $wp_rewrite;
global $wp_the_query;
define( 'DOING_AJAX', true );
include 'wp-load.php';
}
de_globalize_wp();
echo "It actually worked!\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment