populate_network

Populate network settings.

Description

(bool|WP_Error) populate_network( (int) $network_id = 1, (string) $domain = '', (string) $email = '', (string) $site_name = '', (string) $path = '/', (bool) $subdomain_install = false ); 

Returns (bool|WP_Error)

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

Parameters (6)

0. $network_id — Optional. (int) => 1
ID of network to populate.
1. $domain — Optional. (string) => ''
The domain name for the network (eg. example.com).
2. $email — Optional. (string) => ''
Email address for the network administrator.
3. $site_name — Optional. (string) => ''
The name of the network.
4. $path — Optional. (string) => '/'
The path to append to the network's domain name. Default /..
5. $subdomain_install — Optional. (bool) => false
Whether the network is a subdomain install or a subdirectory install. Default false, meaning the network is a subdirectory install.

Usage

  1. if ( !function_exists( 'populate_network' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/schema.php'; 
  3.  
  4. // ID of network to populate. 
  5. $network_id = 1; 
  6.  
  7. // The domain name for the network (eg. "example.com"). 
  8. $domain = ''; 
  9.  
  10. // Email address for the network administrator. 
  11. $email = ''; 
  12.  
  13. // The name of the network. 
  14. $site_name = ''; 
  15.  
  16. // Optional. The path to append to the network's domain name. Default '/'. 
  17. $path = '/'; 
  18.  
  19. // Optional. Whether the network is a subdomain install or a subdirectory install. 
  20. // Default false, meaning the network is a subdirectory install. 
  21. $subdomain_install = false; 
  22.  
  23. // NOTICE! Understand what this does before running. 
  24. $result = populate_network($network_id, $domain, $email, $site_name, $path, $subdomain_install); 
  25.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/schema.php  
  1. function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) { 
  2. global $wpdb, $current_site, $wp_db_version, $wp_rewrite; 
  3.  
  4. $errors = new WP_Error(); 
  5. if ( '' == $domain ) 
  6. $errors->add( 'empty_domain', __( 'You must provide a domain name.' ) ); 
  7. if ( '' == $site_name ) 
  8. $errors->add( 'empty_sitename', __( 'You must provide a name for your network of sites.' ) ); 
  9.  
  10. // Check for network collision. 
  11. if ( $network_id == $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE id = %d", $network_id ) ) ) 
  12. $errors->add( 'siteid_exists', __( 'The network already exists.' ) ); 
  13.  
  14. if ( ! is_email( $email ) ) 
  15. $errors->add( 'invalid_email', __( 'You must provide a valid email address.' ) ); 
  16.  
  17. if ( $errors->get_error_code() ) 
  18. return $errors; 
  19.  
  20. // If a user with the provided email does not exist, default to the current user as the new network admin. 
  21. $site_user = get_user_by( 'email', $email ); 
  22. if ( false === $site_user ) { 
  23. $site_user = wp_get_current_user(); 
  24.  
  25. // Set up site tables. 
  26. $template = get_option( 'template' ); 
  27. $stylesheet = get_option( 'stylesheet' ); 
  28. $allowed_themes = array( $stylesheet => true ); 
  29.  
  30. if ( $template != $stylesheet ) { 
  31. $allowed_themes[ $template ] = true; 
  32.  
  33. if ( WP_DEFAULT_THEME != $stylesheet && WP_DEFAULT_THEME != $template ) { 
  34. $allowed_themes[ WP_DEFAULT_THEME ] = true; 
  35.  
  36. // If WP_DEFAULT_THEME doesn't exist, also whitelist the latest core default theme. 
  37. if ( ! wp_get_theme( WP_DEFAULT_THEME )->exists() ) { 
  38. if ( $core_default = WP_Theme::get_core_default_theme() ) { 
  39. $allowed_themes[ $core_default->get_stylesheet() ] = true; 
  40.  
  41. if ( 1 == $network_id ) { 
  42. $wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path ) ); 
  43. $network_id = $wpdb->insert_id; 
  44. } else { 
  45. $wpdb->insert( $wpdb->site, array( 'domain' => $domain, 'path' => $path, 'id' => $network_id ) ); 
  46.  
  47. wp_cache_delete( 'networks_have_paths', 'site-options' ); 
  48.  
  49. if ( !is_multisite() ) { 
  50. $site_admins = array( $site_user->user_login ); 
  51. $users = get_users( array( 'fields' => array( 'ID', 'user_login' ) ) ); 
  52. if ( $users ) { 
  53. foreach ( $users as $user ) { 
  54. if ( is_super_admin( $user->ID ) && !in_array( $user->user_login, $site_admins ) ) 
  55. $site_admins[] = $user->user_login; 
  56. } else { 
  57. $site_admins = get_site_option( 'site_admins' ); 
  58.  
  59. /** translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */ 
  60. $welcome_email = __( 'Howdy USERNAME,  
  61.  
  62. Your new SITE_NAME site has been successfully set up at: 
  63. BLOG_URL 
  64.  
  65. You can log in to the administrator account with the following information: 
  66.  
  67. Username: USERNAME 
  68. Password: PASSWORD 
  69. Log in here: BLOG_URLwp-login.php 
  70.  
  71. We hope you enjoy your new site. Thanks! 
  72.  
  73. --The Team @ SITE_NAME' ); 
  74.  
  75. $misc_exts = array( 
  76. // Images. 
  77. 'jpg', 'jpeg', 'png', 'gif',  
  78. // Video. 
  79. 'mov', 'avi', 'mpg', '3gp', '3g2',  
  80. // "audio". 
  81. 'midi', 'mid',  
  82. // Miscellaneous. 
  83. 'pdf', 'doc', 'ppt', 'odt', 'pptx', 'docx', 'pps', 'ppsx', 'xls', 'xlsx', 'key',  
  84. ); 
  85. $audio_exts = wp_get_audio_extensions(); 
  86. $video_exts = wp_get_video_extensions(); 
  87. $upload_filetypes = array_unique( array_merge( $misc_exts, $audio_exts, $video_exts ) ); 
  88.  
  89. $sitemeta = array( 
  90. 'site_name' => $site_name,  
  91. 'admin_email' => $email,  
  92. 'admin_user_id' => $site_user->ID,  
  93. 'registration' => 'none',  
  94. 'upload_filetypes' => implode( ' ', $upload_filetypes ),  
  95. 'blog_upload_space' => 100,  
  96. 'fileupload_maxk' => 1500,  
  97. 'site_admins' => $site_admins,  
  98. 'allowedthemes' => $allowed_themes,  
  99. 'illegal_names' => array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),  
  100. 'wpmu_upgrade_site' => $wp_db_version,  
  101. 'welcome_email' => $welcome_email,  
  102. /** translators: %s: site link */ 
  103. 'first_post' => __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' ),  
  104. // @todo - network admins should have a method of editing the network siteurl (used for cookie hash) 
  105. 'siteurl' => get_option( 'siteurl' ) . '/',  
  106. 'add_new_users' => '0',  
  107. 'upload_space_check_disabled' => is_multisite() ? get_site_option( 'upload_space_check_disabled' ) : '1',  
  108. 'subdomain_install' => intval( $subdomain_install ),  
  109. 'ms_files_rewriting' => is_multisite() ? get_site_option( 'ms_files_rewriting' ) : '0',  
  110. 'initial_db_version' => get_option( 'initial_db_version' ),  
  111. 'active_sitewide_plugins' => array(),  
  112. 'WPLANG' => get_locale(),  
  113. ); 
  114. if ( ! $subdomain_install ) 
  115. $sitemeta['illegal_names'][] = 'blog'; 
  116.  
  117. /** 
  118. * Filters meta for a network on creation. 
  119. * @since 3.7.0 
  120. * @param array $sitemeta Associative array of network meta keys and values to be inserted. 
  121. * @param int $network_id ID of network to populate. 
  122. */ 
  123. $sitemeta = apply_filters( 'populate_network_meta', $sitemeta, $network_id ); 
  124.  
  125. $insert = ''; 
  126. foreach ( $sitemeta as $meta_key => $meta_value ) { 
  127. if ( is_array( $meta_value ) ) 
  128. $meta_value = serialize( $meta_value ); 
  129. if ( !empty( $insert ) ) 
  130. $insert .= ', '; 
  131. $insert .= $wpdb->prepare( "( %d, %s, %s)", $network_id, $meta_key, $meta_value ); 
  132. $wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert ); 
  133.  
  134. /** 
  135. * When upgrading from single to multisite, assume the current site will 
  136. * become the main site of the network. When using populate_network() 
  137. * to create another network in an existing multisite environment, skip 
  138. * these steps since the main site of the new network has not yet been 
  139. * created. 
  140. */ 
  141. if ( ! is_multisite() ) { 
  142. $current_site = new stdClass; 
  143. $current_site->domain = $domain; 
  144. $current_site->path = $path; 
  145. $current_site->site_name = ucfirst( $domain ); 
  146. $wpdb->insert( $wpdb->blogs, array( 'site_id' => $network_id, 'blog_id' => 1, 'domain' => $domain, 'path' => $path, 'registered' => current_time( 'mysql' ) ) ); 
  147. $current_site->blog_id = $blog_id = $wpdb->insert_id; 
  148. update_user_meta( $site_user->ID, 'source_domain', $domain ); 
  149. update_user_meta( $site_user->ID, 'primary_blog', $blog_id ); 
  150.  
  151. if ( $subdomain_install ) 
  152. $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 
  153. else 
  154. $wp_rewrite->set_permalink_structure( '/blog/%year%/%monthnum%/%day%/%postname%/' ); 
  155.  
  156.  
  157. if ( ! $subdomain_install ) 
  158. return true; 
  159.  
  160. $vhost_ok = false; 
  161. $errstr = ''; 
  162. $hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname! 
  163. $page = wp_remote_get( 'http://' . $hostname, array( 'timeout' => 5, 'httpversion' => '1.1' ) ); 
  164. if ( is_wp_error( $page ) ) 
  165. $errstr = $page->get_error_message(); 
  166. elseif ( 200 == wp_remote_retrieve_response_code( $page ) ) 
  167. $vhost_ok = true; 
  168.  
  169. if ( ! $vhost_ok ) { 
  170. $msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>'; 
  171.  
  172. $msg .= '<p>' . sprintf( 
  173. /** translators: %s: host name */ 
  174. __( 'The installer attempted to contact a random hostname (%s) on your domain.' ),  
  175. '<code>' . $hostname . '</code>' 
  176. ); 
  177. if ( ! empty ( $errstr ) ) { 
  178. /** translators: %s:errormessage */ 
  179. $msg .= ' ' . sprintf( __( 'This resulted in anerrormessage: %s' ), '<code>' . $errstr . '</code>' ); 
  180. $msg .= '</p>'; 
  181.  
  182. $msg .= '<p>' . sprintf( 
  183. /** translators: %s: asterisk symbol (*) */ 
  184. __( 'To use a subdomain configuration, you must have a wildcard entry in your DNS. This usually means adding a %s hostname record pointing at your web server in your DNS configuration tool.' ),  
  185. '<code>*</code>' 
  186. ) . '</p>'; 
  187.  
  188. $msg .= '<p>' . __( 'You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.' ) . '</p>'; 
  189.  
  190. return new WP_Error( 'no_wildcard_dns', $msg ); 
  191.  
  192. return true;