• Hi, I’m using the latest version of WP, with the multi-site functionality installed and working. I have a fair knowledge of PHP, MySQL, JavaScript, HTML, and CSS. When I run the code to make a new site, I’d like to avoid any output on the page. I’d also like to be able to choose the theme, insert some text into the new site, and give admin (not super-admin) access to a user (it would be better if the script could give access to a user that has already been created). Help with anything I have mentioned is greatly appreciated! 🙂

    I tried “googling it” but couldn’t find anything (although, my “google-fu” is a bit “rusty”). I apologize if there is already another thread about this.

    EDIT: Sorry, forgot to add, I have the “sub-dir” install.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter james108

    (@james108)

    This doesn’t make much sense to me. I copied ‘n pasted some PHP code (at the bottom) from /wp-admin/network/site-new.php and it worked, without any output. I looked at the network admin interface, and it looked like everything was working. So I try to visit the site, and that’s when I get a 404. Anyone know a work around? Here’s the code (minus some details):

    <?php
        /* I have omitted the values of $domain, $sPath, $title, and $uuid.
         * For those who don't understand, $domain is the domain of the site (e.g. wordpress.org ),
         * $sPath is the new site's path, $title is for the website's title,
         * and $uuid is the user's ID from the DB. This was made for the sub-dir install. If you have the sub-domain install, then this script is not what you want.
         */
        require_once( 'wp-config.php' );
        require_once( 'wp-includes/ms-blogs.php' );
        require_once( 'wp-includes/functions.php' );
        require_once( 'wp-admin/includes/upgrade.php' );
        require_once( 'wp-includes/ms-functions.php' );
        wpmu_create_blog( $domain, $sPath, $title, intval( $uuid ), array( 'public' => 1 ) );
    ?>

    Thread Starter james108

    (@james108)

    I found the main problem with the old code – my install was corrupt. I reinstalled WP (this time, I’m sure it’s NOT corrupt.), and the new site that was created told me

    Error establishing a database connection

    so I re-wrote the code (new code at the bottom). Just like the script before, everything worked, until I tried to view the site (and just like before, it told me it couldn’t connect to the DB). I took a look at the DB, and noticed that all of my other sites had their own tables (in the format of (my prefix)_(site’s ID)_options, (my prefix)_(site’s ID)_posts, etc.), but the site I made with this script didn’t have it’s own tables with it’s ID. So, my problem now is that WP won’t make tables for my new site.

    <?php
        /* I have omitted the values of $title, $newSitePath, and $email.
         * If my WP installation was at example.com/mystuff and I wanted to make example.com/mystuff/something
         * and I wanted to assign administration rights to xyz@example.com then I'd type in:
         *     $email = 'james@jamescostian.com';
         *     $newSitePath = 'something';
         * I also need to add in a definition for $title. If I wanted to give this site the title "Randomness" then I'd type in:
         *     $title = 'Randomness';
         * This was made for the sub-dir install.
         * If you have the sub-domain install, then go to:
         * wp-admin/network/site-new.php
         * and use it to figure out how to make your own script.
         */
        require_once( 'wp-config.php' );
        require_once( 'wp-includes/ms-blogs.php' );
        require_once( 'wp-includes/functions.php' );
        require_once( 'wp-admin/includes/upgrade.php' );
        require_once( 'wp-includes/ms-functions.php' );
        $domain = strtolower( $domain );
        $email = sanitize_email( $email );
        $newdomain = $current_site->domain;
        $path = $base.$newSitePath.'/';
        $password = 'N/A';
        $user_id = email_exists( $email );
        $wpdb->hide_errors();
        wpmu_create_blog( $newdomain, $path, $title, $user_id, array( 'public' => 1 ), $current_site->id );
        $wpdb->show_errors();
        if ( !is_super_admin( $user_id ) && !get_user_option( 'primary_blog', $user_id ) )
            update_user_option( $user_id, 'primary_blog', $id, true );
    ?>

    Did you have any luck with this? I had the same problem and found that requiring wp-admin/network/admin.php does the trick. However, that requires an admin to be logged in, which is not really what I wanted.

    alright so the following 2 lines added in the require block made it work for me:

    wp_set_current_user(0,"wp_admin"); //pretend it is the network admin doing this, since regular user can't
    require_once('wp-admin/includes/admin.php'); //load the prefixes for database tables
    Thread Starter james108

    (@james108)

    I tried placing that code at the end of my current require_once section, and it didn’t work for me. Could you tell me what code you used (so that I know the order of everything)?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Add a site to WP from an external PHP script’ is closed to new replies.