Skip to content

Instantly share code, notes, and snippets.

@paulgibbs
Created July 4, 2012 07:37
Show Gist options
  • Save paulgibbs/3045932 to your computer and use it in GitHub Desktop.
Save paulgibbs/3045932 to your computer and use it in GitHub Desktop.
Load WordPress via another PHP script
<?php
$site_name = 'news_blog'; // e.g. "example.com/news_blog"
$site_domain = 'example.com';
/**
* Construct a fake $_SERVER global to get WordPress to load a specific site.
* This avoids alot of messing about with switch_to_blog() and all its pitfalls.
*/
$_SERVER = array(
'HTTP_HOST' => $site_domain,
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => "/{$site_name}/",
'SERVER_NAME' => $site_domain,
);
// Remove all our bespoke variables as they'll be in scope as globals and could affect WordPress
unset( $site_name, $site_domain );
// Pretend that we're executing an AJAX process. This should help WordPress not load all of the things.
define( 'DOING_AJAX', true );
// Stop WordPress doing any of its normal output handling.
define( 'WP_USE_THEMES', false );
// Load WordPress - intentionally using an absolute URL due to issues with relative paths on the CLI.
require( '/opt/wp/blogs/wp-load.php' );
// Do normal WordPress stuff here
?>
@yankeeinlondon
Copy link

Great. Very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment