Skip to content

Instantly share code, notes, and snippets.

@dave1010 dave1010/wp-cli.php
Created Apr 26, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.