Skip to content
Advertisement

How do I get the site id in the same function that created the site?

I’m writing some php functions to work off of endpoints, specficially one that takes the input of a form to create a site within a wp multi-site. In terms of progress, the code below will take the input and create a site with the correct path and domain, but none of the meta information is being added.

  public function create_endpoint($request) {
        $key = $request['key'];

//Capturing user-input from form 
        if ($this->validate_key($key)) {
              $title = $request['name'];
              $slug = $request['slug'];
              $admin_user = $request['admin_user'];
              $site_owner = $request['site_owner'];
              $site_ouc = $request['site_ouc'];
              $registered = $request['registered'];
              $service_tier = $request['service_tier'];
              $service_offering = $request['service_offering'];
              
//Site creation process
                $domain = 'localhost';
                $path = 'wordpress/'.$request['slug'];

                $user_id = get_user_by( 'login', $this->admin_user );

                $network_id = get_network()->id;

                if ( !empty($title) and !empty($domain) and !empty($path) ) {
                  wpmu_create_blog($domain, $path, $title, $user_id);


                  $site_id = get_blog_id_from_url( $domain, $path);

                  //Adding in meta-data
                      update_site_meta( $site_id, 'site_owner' , $site_owner);
                      update_site_meta( $site_id, 'site_ouc' , $site_ouc);
                      update_site_meta( $site_id, 'registered' , $registered);
                      update_site_meta( $site_id, 'service_tier' , $service_tier);
                      update_site_meta( $site_id, 'service_offering' , $service_offering);

                  return $site_id;

                }
                else {
                  return "Not enough information";
                }

        }
         else {
            return $this->invalid_key_message;
        }
    }

The root problem being that when I return the $site_id I always get 0, so the update_site_meta functions have nothing to work with.

if ( !empty($title) and !empty($domain) and !empty($path) ) {
                  wpmu_create_blog($domain, $path, $title, $user_id);

                  return $site_id;

Any insight or thoughts are welcome, I have been working against a wall.

Advertisement

Answer

Looking at he docs it seems wpmu_create_blog should return the blog id… so you could update that part of your code to below:

// save id returned from creating the new site
$site_id = wpmu_create_blog($domain, $path, $title, $user_id);

// delete this line
// $site_id = get_blog_id_from_url( $domain, $path);

//Adding in meta-data
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement