Created
July 4, 2012 07:37
-
-
Save paulgibbs/3045932 to your computer and use it in GitHub Desktop.
Load WordPress via another PHP script
This file contains hidden or 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 | |
$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 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great. Very helpful!