I am merging WordPress into a existing system and require our users to be able to make posts to a multi-site WP install. I have a database table that will link our own member to specific blog IDs and stuff, so there will be no need for user/logins as far as WP is concerned.

What I really need to know is how to run certain WP functions outside of WP itself – on a completely different domain infact (but same server). I have tried simply including wp-load.php in our existing admin panel but as soon as I do it redirects to the main site – I assume because the domains do not match:

domain1.com and domain2.com are both on the same server, domain1.com is the WP MU setup, on domain2.com in our own admin area I am including wp-load.php and as soon as I do it redirects me straight to the homepage of domain1.com.

Is it even preferable to do it this way? I have seen a few examples where people have directly queried the WP database to insert posts. but if that’s the case I have to ask myself why I am even using WP for this project at all?! I am thinking about using WP XMLRPC API but I need more power than that and don’t wish to turn in on really.

2 Answers
2

Ok I have cracked it, by spoofing the $_SERVER variable and pre-defing some constants, I was able to prevent WordPress from redirecting after the inclusion of wp-load.php.

define('WP_USE_THEMES', false);
define( 'DOMAIN_CURRENT_SITE', $siteRow['domain'] );
define( 'PATH_CURRENT_SITE', "https://wordpress.stackexchange.com/" );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', $siteRow['wp_blog_id'] );

$_SERVER = array(
    "HTTP_HOST" => $siteRow['domain'],
    "SERVER_NAME" => $siteRow['domain'],
    "REQUEST_URI" => "https://wordpress.stackexchange.com/",
    "REQUEST_METHOD" => "GET"
);

require_once WP_PATH.'wp-load.php';
switch_to_blog($siteRow['wp_blog_id']);

$siteRow contains details about the target site. Note: This cannot be inside a function due to global variable restraints.

Leave a Reply

Your email address will not be published. Required fields are marked *