Creating a Post form outside of the Admin

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:

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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', '/' );
    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" => "/",
        "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.

  2. Hi
    I am a hacker not a programmer and I was able to get this to work quickly, but I think I was lucky.

    I assumed the “define” commands were what was in my multisite dashboard and I added the $_server code to the top of my wp_insert_post script and it worked.
    I didn’t use the following:

    define('WP_USE_THEMES', false);
    

    I used the following to select the blog although as it was the default blog maybe that wasn’t necessary:

    global $switched; switch_to_blog(1);
    

    Or should I have put the “define” statements into the script?

    Cheers

    Jack