populate_network( int $network_id = 1, string $domain = '', string $email = '', string $site_name = '', string $path = '/', bool $subdomain_install = false )
Populate network settings.
Parameters
- $network_id
-
(Optional) ID of network to populate.
Default value: 1
- $domain
-
(Optional) The domain name for the network. Example: "example.com".
Default value: ''
-
(Optional) Email address for the network administrator.
Default value: ''
- $site_name
-
(Optional) The name of the network.
Default value: ''
- $path
-
(Optional) The path to append to the network's domain name. Default '/'.
Default value: '/'
- $subdomain_install
-
(Optional) Whether the network is a subdomain installation or a subdirectory installation.<br> Default false, meaning the network is a subdirectory installation.
Default value: false
Return
(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: wp-admin/includes/schema.php
function populate_network( $network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false ) {
global $wpdb, $current_site, $wp_db_version, $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->get_error_code() )
return $errors;
// If a user with the provided email does not exist, default to the current user as the new network admin.
$site_user = get_user_by( 'email', $email );
if ( false === $site_user ) {
$site_user = wp_get_current_user();
}
// Set up site tables.
$template = get_option( 'template' );
$stylesheet = get_option( 'stylesheet' );
$allowed_themes = array( $stylesheet => true );
if ( $template != $stylesheet ) {
$allowed_themes[ $template ] = true;
}
if ( WP_DEFAULT_THEME != $stylesheet && WP_DEFAULT_THEME != $template ) {
$allowed_themes[ WP_DEFAULT_THEME ] = true;
}
// If WP_DEFAULT_THEME doesn't exist, also whitelist the latest core default theme.
if ( ! wp_get_theme( WP_DEFAULT_THEME )->exists() ) {
if ( $core_default = WP_Theme::get_core_default_theme() ) {
$allowed_themes[ $core_default->get_stylesheet() ] = true;
}
}
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 ) );
}
wp_cache_delete( 'networks_have_paths', 'site-options' );
if ( !is_multisite() ) {
$site_admins = array( $site_user->user_login );
$users = get_users( array(
'fields' => array( 'user_login' ),
'role' => 'administrator',
) );
if ( $users ) {
foreach ( $users as $user ) {
$site_admins[] = $user->user_login;
}
$site_admins = array_unique( $site_admins );
}
} else {
$site_admins = get_site_option( 'site_admins' );
}
/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
$welcome_email = __( 'Hello USERNAME,
Your new SITE_NAME site has been successfully set up at:
BLOG_URL
You can log in to the administrator account with the following information:
Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLwp-login.php
We hope you enjoy your new site. Thanks!
--The Team @ SITE_NAME' );
$misc_exts = array(
// Images.
'jpg', 'jpeg', 'png', 'gif',
// Video.
'mov', 'avi', 'mpg', '3gp', '3g2',
// "audio".
'midi', 'mid',
// Miscellaneous.
'pdf', 'doc', 'ppt', 'odt', 'pptx', 'docx', 'pps', 'ppsx', 'xls', 'xlsx', 'key',
);
$audio_exts = wp_get_audio_extensions();
$video_exts = wp_get_video_extensions();
$upload_filetypes = array_unique( array_merge( $misc_exts, $audio_exts, $video_exts ) );
$sitemeta = array(
'site_name' => $site_name,
'admin_email' => $email,
'admin_user_id' => $site_user->ID,
'registration' => 'none',
'upload_filetypes' => implode( ' ', $upload_filetypes ),
'blog_upload_space' => 100,
'fileupload_maxk' => 1500,
'site_admins' => $site_admins,
'allowedthemes' => $allowed_themes,
'illegal_names' => array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files' ),
'wpmu_upgrade_site' => $wp_db_version,
'welcome_email' => $welcome_email,
/* translators: %s: site link */
'first_post' => __( 'Welcome to %s. This is your first post. Edit or delete it, then start blogging!' ),
// @todo - network admins should have a method of editing the network siteurl (used for cookie hash)
'siteurl' => get_option( 'siteurl' ) . '/',
'add_new_users' => '0',
'upload_space_check_disabled' => is_multisite() ? get_site_option( 'upload_space_check_disabled' ) : '1',
'subdomain_install' => intval( $subdomain_install ),
'global_terms_enabled' => global_terms_enabled() ? '1' : '0',
'ms_files_rewriting' => is_multisite() ? get_site_option( 'ms_files_rewriting' ) : '0',
'initial_db_version' => get_option( 'initial_db_version' ),
'active_sitewide_plugins' => array(),
'WPLANG' => get_locale(),
);
if ( ! $subdomain_install )
$sitemeta['illegal_names'][] = 'blog';
/**
* Filters meta for a network on creation.
*
* @since WP-3.7.0
*
* @param array $sitemeta Associative array of network meta keys and values to be inserted.
* @param int $network_id ID of network to populate.
*/
$sitemeta = apply_filters( 'populate_network_meta', $sitemeta, $network_id );
$insert = '';
foreach ( $sitemeta as $meta_key => $meta_value ) {
if ( is_array( $meta_value ) )
$meta_value = serialize( $meta_value );
if ( !empty( $insert ) )
$insert .= ', ';
$insert .= $wpdb->prepare( "( %d, %s, %s)", $network_id, $meta_key, $meta_value );
}
$wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert );
/*
* 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 = $blog_id = $wpdb->insert_id;
update_user_meta( $site_user->ID, 'source_domain', $domain );
update_user_meta( $site_user->ID, 'primary_blog', $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;
$vhost_ok = false;
$errstr = '';
$hostname = substr( md5( time() ), 0, 6 ) . '.' . $domain; // Very random hostname!
$page = wp_remote_get( 'http://' . $hostname, array( 'timeout' => 5, 'httpversion' => '1.1' ) );
if ( is_wp_error( $page ) )
$errstr = $page->get_error_message();
elseif ( 200 == wp_remote_retrieve_response_code( $page ) )
$vhost_ok = true;
if ( ! $vhost_ok ) {
$msg = '<p><strong>' . __( 'Warning! Wildcard DNS may not be configured correctly!' ) . '</strong></p>';
$msg .= '<p>' . sprintf(
/* translators: %s: host name */
__( 'The installer attempted to contact a random hostname (%s) on your domain.' ),
'<code>' . $hostname . '</code>'
);
if ( ! empty ( $errstr ) ) {
/* translators: %s: error message */
$msg .= ' ' . sprintf( __( 'This resulted in an error message: %s' ), '<code>' . $errstr . '</code>' );
}
$msg .= '</p>';
$msg .= '<p>' . sprintf(
/* translators: %s: asterisk symbol (*) */
__( '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.' ),
'<code>*</code>'
) . '</p>';
$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>';
return new WP_Error( 'no_wildcard_dns', $msg );
}
}
return true;
}
Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |