populate_network() – Populate network settings.

You appear to be a bot. Output may be restricted

Description

Populate network settings.

Usage

$bool|WP_Error = populate_network( $network_id, $domain, $email, $site_name, $path, $subdomain_install );

Parameters

$network_id
( int ) optional default: 1 – ID of network to populate.
$domain
( string ) optional – The domain name for the network. Example: "example.com".
$email
( string ) optional – Email address for the network administrator.
$site_name
( string ) optional – The name of the network.
$path
( string ) optional default: / – Optional. The path to append to the network's domain name. Default '/'.
$subdomain_install
( bool ) optional – Optional. Whether the network is a subdomain installation or a subdirectory installation. Default false, meaning the network is a subdirectory installation.

Returns

bool|WP_Error True on success, or WP_Error on warning (with the installation otherwise successful, so the error code must be checked) or failure.

Source

File name: wordpress/wp-admin/includes/schema.php
Lines:

1 to 100 of 144
function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
  global $wpdb, $current_site, $wp_rewrite;

  $errors = new WP_Error();
  if ( '' === $domain ) {
    $errors->add( 'empty_domain', __( 'You must provide a domain name.' ) );
  }
  if ( '' === $site_name ) {
    $errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) );
  }

  // Check for network collision.
  $network_exists = false;
  if ( is_multisite() ) {
    if ( get_network( (int) $network_id ) ) {
      $errors->add( 'siteid_exists', __( 'The network already exists.' ) );
    }
  } else {
    if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) ) {
      $errors->add( 'siteid_exists', __( 'The network already exists.' ) );
    }
  }

  if ( ! is_email( $email ) ) {
    $errors->add( 'invalid_email', __( 'You must provide a valid email address.' ) );
  }

  if ( $errors->has_errors() ) {
    return $errors;
  }

  if ( 1 == $network_id ) {
    $wpdb->insert(
      $wpdb->site,
      array(
        'domain' => $domain,
        'path'   => $path,
      )
    );
    $network_id = $wpdb->insert_id;
  } else {
    $wpdb->insert(
      $wpdb->site,
      array(
        'domain' => $domain,
        'path'   => $path,
        'id'     => $network_id,
      )
    );
  }

  populate_network_meta(
    $network_id,
    array(
      'admin_email'       => $email,
      'site_name'         => $site_name,
      'subdomain_install' => $subdomain_install,
    )
  );

  $site_user = get_userdata( (int) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", 'admin_user_id', $network_id ) ) );

  /*
	 * When upgrading from single to multisite, assume the current site will
	 * become the main site of the network. When using populate_network()
	 * to create another network in an existing multisite environment, skip
	 * these steps since the main site of the new network has not yet been
	 * created.
	 */
  if ( ! is_multisite() ) {
    $current_site            = new stdClass;
    $current_site->domain    = $domain;
    $current_site->path      = $path;
    $current_site->site_name = ucfirst( $domain );
    $wpdb->insert(
      $wpdb->blogs,
      array(
        'site_id'    => $network_id,
        'blog_id'    => 1,
        'domain'     => $domain,
        'path'       => $path,
        'registered' => current_time( 'mysql' ),
      )
    );
    $current_site->blog_id = $wpdb->insert_id;
    update_user_meta( $site_user->ID, 'source_domain', $domain );
    update_user_meta( $site_user->ID, 'primary_blog', $current_site->blog_id );

    if ( $subdomain_install ) {
      $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    } else {
      $wp_rewrite->set_permalink_structure( '/blog/%year%/%monthnum%/%day%/%postname%/' );
    }

    flush_rewrite_rules();

    if ( ! $subdomain_install ) {
      return true;
    }

 

 View on GitHub View on Trac