3 * WordPress Network Administration API.
6 * @subpackage Administration
11 * Check for an existing network.
15 * @global wpdb $wpdb WordPress database abstraction object.
17 * @return Whether a network exists.
19 function network_domain_check() {
22 $sql = $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $wpdb->site ) );
23 if ( $wpdb->get_var( $sql ) ) {
24 return $wpdb->get_var( "SELECT domain FROM $wpdb->site ORDER BY id ASC LIMIT 1" );
30 * Allow subdomain install
33 * @return bool Whether subdomain install is allowed
35 function allow_subdomain_install() {
36 $domain = preg_replace( '|https?://([^/]+)|', '$1', get_option( 'home' ) );
37 if ( parse_url( get_option( 'home' ), PHP_URL_PATH ) || 'localhost' == $domain || preg_match( '|^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$|', $domain ) )
44 * Allow subdirectory install.
48 * @global wpdb $wpdb WordPress database abstraction object.
50 * @return bool Whether subdirectory install is allowed
52 function allow_subdirectory_install() {
55 * Filters whether to enable the subdirectory install feature in Multisite.
59 * @param bool $allow Whether to enable the subdirectory install feature in Multisite. Default is false.
61 if ( apply_filters( 'allow_subdirectory_install', false ) )
64 if ( defined( 'ALLOW_SUBDIRECTORY_INSTALL' ) && ALLOW_SUBDIRECTORY_INSTALL )
67 $post = $wpdb->get_row( "SELECT ID FROM $wpdb->posts WHERE post_date < DATE_SUB(NOW(), INTERVAL 1 MONTH) AND post_status = 'publish'" );
75 * Get base domain of network.
78 * @return string Base domain.
80 function get_clean_basedomain() {
81 if ( $existing_domain = network_domain_check() )
82 return $existing_domain;
83 $domain = preg_replace( '|https?://|', '', get_option( 'siteurl' ) );
84 if ( $slash = strpos( $domain, '/' ) )
85 $domain = substr( $domain, 0, $slash );
90 * Prints step 1 for Network installation process.
92 * @todo Realistically, step 1 should be a welcome screen explaining what a Network is and such. Navigating to Tools > Network
93 * should not be a sudden "Welcome to a new install process! Fill this out and click here." See also contextual help todo.
97 * @global bool $is_apache
99 * @param WP_Error $errors
101 function network_step1( $errors = false ) {
104 if ( defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
105 echo '<div class="error"><p><strong>' . __('ERROR:') . '</strong> ' . __( 'The constant DO_NOT_UPGRADE_GLOBAL_TABLES cannot be defined when creating a network.' ) . '</p></div>';
107 include( ABSPATH . 'wp-admin/admin-footer.php' );
111 $active_plugins = get_option( 'active_plugins' );
112 if ( ! empty( $active_plugins ) ) {
113 echo '<div class="updated"><p><strong>' . __('Warning:') . '</strong> ' . sprintf( __( 'Please <a href="%s">deactivate your plugins</a> before enabling the Network feature.' ), admin_url( 'plugins.php?plugin_status=active' ) ) . '</p></div><p>' . __( 'Once the network is created, you may reactivate your plugins.' ) . '</p>';
115 include( ABSPATH . 'wp-admin/admin-footer.php' );
119 $hostname = get_clean_basedomain();
120 $has_ports = strstr( $hostname, ':' );
121 if ( ( false !== $has_ports && ! in_array( $has_ports, array( ':80', ':443' ) ) ) ) {
122 echo '<div class="error"><p><strong>' . __( 'ERROR:') . '</strong> ' . __( 'You cannot install a network of sites with your server address.' ) . '</p></div>';
123 echo '<p>' . sprintf(
124 /* translators: %s: port number */
125 __( 'You cannot use port numbers such as %s.' ),
126 '<code>' . $has_ports . '</code>'
128 echo '<a href="' . esc_url( admin_url() ) . '">' . __( 'Return to Dashboard' ) . '</a>';
130 include( ABSPATH . 'wp-admin/admin-footer.php' );
134 echo '<form method="post">';
136 wp_nonce_field( 'install-network-1' );
138 $error_codes = array();
139 if ( is_wp_error( $errors ) ) {
140 echo '<div class="error"><p><strong>' . __( 'ERROR: The network could not be created.' ) . '</strong></p>';
141 foreach ( $errors->get_error_messages() as $error )
142 echo "<p>$error</p>";
144 $error_codes = $errors->get_error_codes();
147 if ( ! empty( $_POST['sitename'] ) && ! in_array( 'empty_sitename', $error_codes ) ) {
148 $site_name = $_POST['sitename'];
150 /* translators: %s: Default network name */
151 $site_name = sprintf( __( '%s Sites' ), get_option( 'blogname' ) );
154 if ( ! empty( $_POST['email'] ) && ! in_array( 'invalid_email', $error_codes ) ) {
155 $admin_email = $_POST['email'];
157 $admin_email = get_option( 'admin_email' );
160 <p><?php _e( 'Welcome to the Network installation process!' ); ?></p>
161 <p><?php _e( 'Fill in the information below and you’ll be on your way to creating a network of WordPress sites. We will create configuration files in the next step.' ); ?></p>
164 if ( isset( $_POST['subdomain_install'] ) ) {
165 $subdomain_install = (bool) $_POST['subdomain_install'];
166 } elseif ( apache_mod_loaded('mod_rewrite') ) { // assume nothing
167 $subdomain_install = true;
168 } elseif ( !allow_subdirectory_install() ) {
169 $subdomain_install = true;
171 $subdomain_install = false;
172 if ( $got_mod_rewrite = got_mod_rewrite() ) { // dangerous assumptions
173 echo '<div class="updated inline"><p><strong>' . __( 'Note:' ) . '</strong> ';
174 /* translators: %s: mod_rewrite */
175 printf( __( 'Please make sure the Apache %s module is installed as it will be used at the end of this installation.' ),
176 '<code>mod_rewrite</code>'
179 } elseif ( $is_apache ) {
180 echo '<div class="error inline"><p><strong>' . __( 'Warning!' ) . '</strong> ';
181 /* translators: %s: mod_rewrite */
182 printf( __( 'It looks like the Apache %s module is not installed.' ),
183 '<code>mod_rewrite</code>'
188 if ( $got_mod_rewrite || $is_apache ) { // Protect against mod_rewrite mimicry (but ! Apache)
190 /* translators: 1: mod_rewrite, 2: mod_rewrite documentation URL, 3: Google search for mod_rewrite */
191 printf( __( 'If %1$s is disabled, ask your administrator to enable that module, or look at the <a href="%2$s">Apache documentation</a> or <a href="%3$s">elsewhere</a> for help setting it up.' ),
192 '<code>mod_rewrite</code>',
193 'https://httpd.apache.org/docs/mod/mod_rewrite.html',
194 'https://www.google.com/search?q=apache+mod_rewrite'
200 if ( allow_subdomain_install() && allow_subdirectory_install() ) : ?>
201 <h3><?php esc_html_e( 'Addresses of Sites in your Network' ); ?></h3>
202 <p><?php _e( 'Please choose whether you would like sites in your WordPress network to use sub-domains or sub-directories.' ); ?>
203 <strong><?php _e( 'You cannot change this later.' ); ?></strong></p>
204 <p><?php _e( 'You will need a wildcard DNS record if you are going to use the virtual host (sub-domain) functionality.' ); ?></p>
205 <?php // @todo: Link to an MS readme? ?>
206 <table class="form-table">
208 <th><label><input type="radio" name="subdomain_install" value="1"<?php checked( $subdomain_install ); ?> /> <?php _e( 'Sub-domains' ); ?></label></th>
210 /* translators: 1: hostname */
211 _x( 'like <code>site1.%1$s</code> and <code>site2.%1$s</code>', 'subdomain examples' ),
216 <th><label><input type="radio" name="subdomain_install" value="0"<?php checked( ! $subdomain_install ); ?> /> <?php _e( 'Sub-directories' ); ?></label></th>
218 /* translators: 1: hostname */
219 _x( 'like <code>%1$s/site1</code> and <code>%1$s/site2</code>', 'subdirectory examples' ),
228 if ( WP_CONTENT_DIR != ABSPATH . 'wp-content' && ( allow_subdirectory_install() || ! allow_subdomain_install() ) )
229 echo '<div class="error inline"><p><strong>' . __('Warning!') . '</strong> ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</p></div>';
231 $is_www = ( 0 === strpos( $hostname, 'www.' ) );
234 <h3><?php esc_html_e( 'Server Address' ); ?></h3>
236 /* translators: 1: site url 2: host name 3. www */
237 __( 'We recommend you change your siteurl to %1$s before enabling the network feature. It will still be possible to visit your site using the %3$s prefix with an address like %2$s but any links will not have the %3$s prefix.' ),
238 '<code>' . substr( $hostname, 4 ) . '</code>',
239 '<code>' . $hostname . '</code>',
242 <table class="form-table">
244 <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
247 /* translators: %s: host name */
248 __( 'The internet address of your network will be %s.' ),
249 '<code>' . $hostname . '</code>'
256 <h3><?php esc_html_e( 'Network Details' ); ?></h3>
257 <table class="form-table">
258 <?php if ( 'localhost' == $hostname ) : ?>
260 <th scope="row"><?php esc_html_e( 'Sub-directory Install' ); ?></th>
263 /* translators: 1: localhost 2: localhost.localdomain */
264 __( 'Because you are using %1$s, the sites in your WordPress network must use sub-directories. Consider using %2$s if you wish to use sub-domains.' ),
265 '<code>localhost</code>',
266 '<code>localhost.localdomain</code>'
269 if ( !allow_subdirectory_install() )
270 echo ' <strong>' . __( 'Warning!' ) . ' ' . __( 'The main site in a sub-directory install will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
273 <?php elseif ( !allow_subdomain_install() ) : ?>
275 <th scope="row"><?php esc_html_e( 'Sub-directory Install' ); ?></th>
277 _e( 'Because your install is in a directory, the sites in your WordPress network must use sub-directories.' );
279 if ( !allow_subdirectory_install() )
280 echo ' <strong>' . __( 'Warning!' ) . ' ' . __( 'The main site in a sub-directory install will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
283 <?php elseif ( !allow_subdirectory_install() ) : ?>
285 <th scope="row"><?php esc_html_e( 'Sub-domain Install' ); ?></th>
286 <td><?php _e( 'Because your install is not new, the sites in your WordPress network must use sub-domains.' );
287 echo ' <strong>' . __( 'The main site in a sub-directory install will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
291 <?php if ( ! $is_www ) : ?>
293 <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
296 /* translators: %s: host name */
297 __( 'The internet address of your network will be %s.' ),
298 '<code>' . $hostname . '</code>'
304 <th scope='row'><?php esc_html_e( 'Network Title' ); ?></th>
306 <input name='sitename' type='text' size='45' value='<?php echo esc_attr( $site_name ); ?>' />
307 <p class="description">
308 <?php _e( 'What would you like to call your network?' ); ?>
313 <th scope='row'><?php esc_html_e( 'Network Admin Email' ); ?></th>
315 <input name='email' type='text' size='45' value='<?php echo esc_attr( $admin_email ); ?>' />
316 <p class="description">
317 <?php _e( 'Your email address.' ); ?>
322 <?php submit_button( __( 'Install' ), 'primary', 'submit' ); ?>
328 * Prints step 2 for Network installation process.
332 * @global wpdb $wpdb WordPress database abstraction object.
334 * @param WP_Error $errors
336 function network_step2( $errors = false ) {
339 $hostname = get_clean_basedomain();
340 $slashed_home = trailingslashit( get_option( 'home' ) );
341 $base = parse_url( $slashed_home, PHP_URL_PATH );
342 $document_root_fix = str_replace( '\\', '/', realpath( $_SERVER['DOCUMENT_ROOT'] ) );
343 $abspath_fix = str_replace( '\\', '/', ABSPATH );
344 $home_path = 0 === strpos( $abspath_fix, $document_root_fix ) ? $document_root_fix . $base : get_home_path();
345 $wp_siteurl_subdir = preg_replace( '#^' . preg_quote( $home_path, '#' ) . '#', '', $abspath_fix );
346 $rewrite_base = ! empty( $wp_siteurl_subdir ) ? ltrim( trailingslashit( $wp_siteurl_subdir ), '/' ) : '';
349 $location_of_wp_config = $abspath_fix;
350 if ( ! file_exists( ABSPATH . 'wp-config.php' ) && file_exists( dirname( ABSPATH ) . '/wp-config.php' ) ) {
351 $location_of_wp_config = dirname( $abspath_fix );
353 $location_of_wp_config = trailingslashit( $location_of_wp_config );
355 // Wildcard DNS message.
356 if ( is_wp_error( $errors ) )
357 echo '<div class="error">' . $errors->get_error_message() . '</div>';
360 if ( allow_subdomain_install() )
361 $subdomain_install = allow_subdirectory_install() ? ! empty( $_POST['subdomain_install'] ) : true;
363 $subdomain_install = false;
365 if ( is_multisite() ) {
366 $subdomain_install = is_subdomain_install();
368 <p><?php _e( 'The original configuration steps are shown here for reference.' ); ?></p>
371 $subdomain_install = (bool) $wpdb->get_var( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = 1 AND meta_key = 'subdomain_install'" );
373 <div class="error"><p><strong><?php _e('Warning:'); ?></strong> <?php _e( 'An existing WordPress network was detected.' ); ?></p></div>
374 <p><?php _e( 'Please complete the configuration steps. To create a new network, you will need to empty or remove the network database tables.' ); ?></p>
379 $subdir_match = $subdomain_install ? '' : '([_0-9a-zA-Z-]+/)?';
380 $subdir_replacement_01 = $subdomain_install ? '' : '$1';
381 $subdir_replacement_12 = $subdomain_install ? '$1' : '$2';
383 if ( $_POST || ! is_multisite() ) {
385 <h3><?php esc_html_e( 'Enabling the Network' ); ?></h3>
386 <p><?php _e( 'Complete the following steps to enable the features for creating a network of sites.' ); ?></p>
387 <div class="updated inline"><p><?php
388 if ( file_exists( $home_path . '.htaccess' ) ) {
389 echo '<strong>' . __( 'Caution:' ) . '</strong> ';
391 /* translators: 1: wp-config.php 2: .htaccess */
392 __( 'We recommend you back up your existing %1$s and %2$s files.' ),
393 '<code>wp-config.php</code>',
394 '<code>.htaccess</code>'
396 } elseif ( file_exists( $home_path . 'web.config' ) ) {
397 echo '<strong>' . __( 'Caution:' ) . '</strong> ';
399 /* translators: 1: wp-config.php 2: web.config */
400 __( 'We recommend you back up your existing %1$s and %2$s files.' ),
401 '<code>wp-config.php</code>',
402 '<code>web.config</code>'
405 echo '<strong>' . __( 'Caution:' ) . '</strong> ';
407 /* translators: 1: wp-config.php */
408 __( 'We recommend you back up your existing %s file.' ),
409 '<code>wp-config.php</code>'
418 /* translators: 1: wp-config.php 2: location of wp-config file, 3: translated version of "That's all, stop editing! Happy blogging." */
419 __( 'Add the following to your %1$s file in %2$s <strong>above</strong> the line reading %3$s:' ),
420 '<code>wp-config.php</code>',
421 '<code>' . $location_of_wp_config . '</code>',
423 * translators: This string should only be translated if wp-config-sample.php is localized.
424 * You can check the localized release package or
425 * https://i18n.svn.wordpress.org/<locale code>/branches/<wp version>/dist/wp-config-sample.php
427 '<code>/* ' . __( 'That’s all, stop editing! Happy blogging.' ) . ' */</code>'
429 <textarea class="code" readonly="readonly" cols="100" rows="7">
430 define('MULTISITE', true);
431 define('SUBDOMAIN_INSTALL', <?php echo $subdomain_install ? 'true' : 'false'; ?>);
432 define('DOMAIN_CURRENT_SITE', '<?php echo $hostname; ?>');
433 define('PATH_CURRENT_SITE', '<?php echo $base; ?>');
434 define('SITE_ID_CURRENT_SITE', 1);
435 define('BLOG_ID_CURRENT_SITE', 1);
438 $keys_salts = array( 'AUTH_KEY' => '', 'SECURE_AUTH_KEY' => '', 'LOGGED_IN_KEY' => '', 'NONCE_KEY' => '', 'AUTH_SALT' => '', 'SECURE_AUTH_SALT' => '', 'LOGGED_IN_SALT' => '', 'NONCE_SALT' => '' );
439 foreach ( $keys_salts as $c => $v ) {
441 unset( $keys_salts[ $c ] );
444 if ( ! empty( $keys_salts ) ) {
445 $keys_salts_str = '';
446 $from_api = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
447 if ( is_wp_error( $from_api ) ) {
448 foreach ( $keys_salts as $c => $v ) {
449 $keys_salts_str .= "\ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
452 $from_api = explode( "\n", wp_remote_retrieve_body( $from_api ) );
453 foreach ( $keys_salts as $c => $v ) {
454 $keys_salts_str .= "\ndefine( '$c', '" . substr( array_shift( $from_api ), 28, 64 ) . "' );";
457 $num_keys_salts = count( $keys_salts );
461 if ( 1 == $num_keys_salts ) {
463 /* translators: 1: wp-config.php */
464 __( 'This unique authentication key is also missing from your %s file.' ),
465 '<code>wp-config.php</code>'
469 /* translators: 1: wp-config.php */
470 __( 'These unique authentication keys are also missing from your %s file.' ),
471 '<code>wp-config.php</code>'
475 <?php _e( 'To make your installation more secure, you should also add:' ); ?>
477 <textarea class="code" readonly="readonly" cols="100" rows="<?php echo $num_keys_salts; ?>"><?php echo esc_textarea( $keys_salts_str ); ?></textarea>
483 if ( iis7_supports_permalinks() ) :
484 // IIS doesn't support RewriteBase, all your RewriteBase are belong to us
485 $iis_subdir_match = ltrim( $base, '/' ) . $subdir_match;
486 $iis_rewrite_base = ltrim( $base, '/' ) . $rewrite_base;
487 $iis_subdir_replacement = $subdomain_install ? '' : '{R:1}';
489 $web_config_file = '<?xml version="1.0" encoding="UTF-8"?>
494 <rule name="WordPress Rule 1" stopProcessing="true">
495 <match url="^index\.php$" ignoreCase="false" />
496 <action type="None" />
498 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
499 $web_config_file .= '
500 <rule name="WordPress Rule for Files" stopProcessing="true">
501 <match url="^' . $iis_subdir_match . 'files/(.+)" ignoreCase="false" />
502 <action type="Rewrite" url="' . $iis_rewrite_base . WPINC . '/ms-files.php?file={R:1}" appendQueryString="false" />
505 $web_config_file .= '
506 <rule name="WordPress Rule 2" stopProcessing="true">
507 <match url="^' . $iis_subdir_match . 'wp-admin$" ignoreCase="false" />
508 <action type="Redirect" url="' . $iis_subdir_replacement . 'wp-admin/" redirectType="Permanent" />
510 <rule name="WordPress Rule 3" stopProcessing="true">
511 <match url="^" ignoreCase="false" />
512 <conditions logicalGrouping="MatchAny">
513 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
514 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
516 <action type="None" />
518 <rule name="WordPress Rule 4" stopProcessing="true">
519 <match url="^' . $iis_subdir_match . '(wp-(content|admin|includes).*)" ignoreCase="false" />
520 <action type="Rewrite" url="' . $iis_rewrite_base . '{R:1}" />
522 <rule name="WordPress Rule 5" stopProcessing="true">
523 <match url="^' . $iis_subdir_match . '([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
524 <action type="Rewrite" url="' . $iis_rewrite_base . '{R:2}" />
526 <rule name="WordPress Rule 6" stopProcessing="true">
527 <match url="." ignoreCase="false" />
528 <action type="Rewrite" url="index.php" />
538 /* translators: 1: a filename like .htaccess. 2: a file path. */
539 __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
540 '<code>web.config</code>',
541 '<code>' . $home_path . '</code>'
544 if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
545 echo '<p><strong>' . __('Warning:') . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
547 <textarea class="code" readonly="readonly" cols="100" rows="20"><?php echo esc_textarea( $web_config_file ); ?>
551 <?php else : // end iis7_supports_permalinks(). construct an htaccess file instead:
553 $ms_files_rewriting = '';
554 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
555 $ms_files_rewriting = "\n# uploaded files\nRewriteRule ^";
556 $ms_files_rewriting .= $subdir_match . "files/(.+) {$rewrite_base}" . WPINC . "/ms-files.php?file={$subdir_replacement_12} [L]" . "\n";
559 $htaccess_file = <<<EOF
562 RewriteRule ^index\.php$ - [L]
563 {$ms_files_rewriting}
564 # add a trailing slash to /wp-admin
565 RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement_01}wp-admin/ [R=301,L]
567 RewriteCond %{REQUEST_FILENAME} -f [OR]
568 RewriteCond %{REQUEST_FILENAME} -d
570 RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}{$subdir_replacement_12} [L]
571 RewriteRule ^{$subdir_match}(.*\.php)$ {$rewrite_base}$subdir_replacement_12 [L]
572 RewriteRule . index.php [L]
578 /* translators: 1: a filename like .htaccess. 2: a file path. */
579 __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
580 '<code>.htaccess</code>',
581 '<code>' . $home_path . '</code>'
584 if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
585 echo '<p><strong>' . __('Warning:') . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
587 <textarea class="code" readonly="readonly" cols="100" rows="<?php echo substr_count( $htaccess_file, "\n" ) + 1; ?>">
588 <?php echo esc_textarea( $htaccess_file ); ?></textarea></li>
591 <?php endif; // end IIS/Apache code branches.
593 if ( !is_multisite() ) { ?>
594 <p><?php _e( 'Once you complete these steps, your network is enabled and configured. You will have to log in again.' ); ?> <a href="<?php echo esc_url( wp_login_url() ); ?>"><?php _e( 'Log In' ); ?></a></p>