Created
April 26, 2011 16:01
-
-
Save dave1010/942539 to your computer and use it in GitHub Desktop.
Load WordPress from a CLI not in the global scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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