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 $site_name = ( ! empty( $_POST['sitename'] ) && ! in_array( 'empty_sitename', $error_codes ) ) ? $_POST['sitename'] : sprintf( _x('%s Sites', 'Default network name' ), get_option( 'blogname' ) );
148 $admin_email = ( ! empty( $_POST['email'] ) && ! in_array( 'invalid_email', $error_codes ) ) ? $_POST['email'] : get_option( 'admin_email' );
150 <p><?php _e( 'Welcome to the Network installation process!' ); ?></p>
151 <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>
154 if ( isset( $_POST['subdomain_install'] ) ) {
155 $subdomain_install = (bool) $_POST['subdomain_install'];
156 } elseif ( apache_mod_loaded('mod_rewrite') ) { // assume nothing
157 $subdomain_install = true;
158 } elseif ( !allow_subdirectory_install() ) {
159 $subdomain_install = true;
161 $subdomain_install = false;
162 if ( $got_mod_rewrite = got_mod_rewrite() ) { // dangerous assumptions
163 echo '<div class="updated inline"><p><strong>' . __( 'Note:' ) . '</strong> ';
164 /* translators: %s: mod_rewrite */
165 printf( __( 'Please make sure the Apache %s module is installed as it will be used at the end of this installation.' ),
166 '<code>mod_rewrite</code>'
169 } elseif ( $is_apache ) {
170 echo '<div class="error inline"><p><strong>' . __( 'Warning!' ) . '</strong> ';
171 /* translators: %s: mod_rewrite */
172 printf( __( 'It looks like the Apache %s module is not installed.' ),
173 '<code>mod_rewrite</code>'
178 if ( $got_mod_rewrite || $is_apache ) { // Protect against mod_rewrite mimicry (but ! Apache)
180 /* translators: 1: mod_rewrite, 2: mod_rewrite documentation URL, 3: Google search for mod_rewrite */
181 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.' ),
182 '<code>mod_rewrite</code>',
183 'https://httpd.apache.org/docs/mod/mod_rewrite.html',
184 'https://www.google.com/search?q=apache+mod_rewrite'
190 if ( allow_subdomain_install() && allow_subdirectory_install() ) : ?>
191 <h3><?php esc_html_e( 'Addresses of Sites in your Network' ); ?></h3>
192 <p><?php _e( 'Please choose whether you would like sites in your WordPress network to use sub-domains or sub-directories.' ); ?>
193 <strong><?php _e( 'You cannot change this later.' ); ?></strong></p>
194 <p><?php _e( 'You will need a wildcard DNS record if you are going to use the virtual host (sub-domain) functionality.' ); ?></p>
195 <?php // @todo: Link to an MS readme? ?>
196 <table class="form-table">
198 <th><label><input type="radio" name="subdomain_install" value="1"<?php checked( $subdomain_install ); ?> /> <?php _e( 'Sub-domains' ); ?></label></th>
200 /* translators: 1: hostname */
201 _x( 'like <code>site1.%1$s</code> and <code>site2.%1$s</code>', 'subdomain examples' ),
206 <th><label><input type="radio" name="subdomain_install" value="0"<?php checked( ! $subdomain_install ); ?> /> <?php _e( 'Sub-directories' ); ?></label></th>
208 /* translators: 1: hostname */
209 _x( 'like <code>%1$s/site1</code> and <code>%1$s/site2</code>', 'subdirectory examples' ),
218 if ( WP_CONTENT_DIR != ABSPATH . 'wp-content' && ( allow_subdirectory_install() || ! allow_subdomain_install() ) )
219 echo '<div class="error inline"><p><strong>' . __('Warning!') . '</strong> ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</p></div>';
221 $is_www = ( 0 === strpos( $hostname, 'www.' ) );
224 <h3><?php esc_html_e( 'Server Address' ); ?></h3>
226 /* translators: 1: site url 2: host name 3. www */
227 __( '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.' ),
228 '<code>' . substr( $hostname, 4 ) . '</code>',
229 '<code>' . $hostname . '</code>',
232 <table class="form-table">
234 <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
237 /* translators: %s: host name */
238 __( 'The internet address of your network will be %s.' ),
239 '<code>' . $hostname . '</code>'
246 <h3><?php esc_html_e( 'Network Details' ); ?></h3>
247 <table class="form-table">
248 <?php if ( 'localhost' == $hostname ) : ?>
250 <th scope="row"><?php esc_html_e( 'Sub-directory Install' ); ?></th>
253 /* translators: 1: localhost 2: localhost.localdomain */
254 __( '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.' ),
255 '<code>localhost</code>',
256 '<code>localhost.localdomain</code>'
259 if ( !allow_subdirectory_install() )
260 echo ' <strong>' . __( 'Warning!' ) . ' ' . __( 'The main site in a sub-directory install will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
263 <?php elseif ( !allow_subdomain_install() ) : ?>
265 <th scope="row"><?php esc_html_e( 'Sub-directory Install' ); ?></th>
267 _e( 'Because your install is in a directory, the sites in your WordPress network must use sub-directories.' );
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_subdirectory_install() ) : ?>
275 <th scope="row"><?php esc_html_e( 'Sub-domain Install' ); ?></th>
276 <td><?php _e( 'Because your install is not new, the sites in your WordPress network must use sub-domains.' );
277 echo ' <strong>' . __( 'The main site in a sub-directory install will need to use a modified permalink structure, potentially breaking existing links.' ) . '</strong>';
281 <?php if ( ! $is_www ) : ?>
283 <th scope='row'><?php esc_html_e( 'Server Address' ); ?></th>
286 /* translators: %s: host name */
287 __( 'The internet address of your network will be %s.' ),
288 '<code>' . $hostname . '</code>'
294 <th scope='row'><?php esc_html_e( 'Network Title' ); ?></th>
296 <input name='sitename' type='text' size='45' value='<?php echo esc_attr( $site_name ); ?>' />
297 <p class="description">
298 <?php _e( 'What would you like to call your network?' ); ?>
303 <th scope='row'><?php esc_html_e( 'Network Admin Email' ); ?></th>
305 <input name='email' type='text' size='45' value='<?php echo esc_attr( $admin_email ); ?>' />
306 <p class="description">
307 <?php _e( 'Your email address.' ); ?>
312 <?php submit_button( __( 'Install' ), 'primary', 'submit' ); ?>
318 * Prints step 2 for Network installation process.
322 * @global wpdb $wpdb WordPress database abstraction object.
324 * @param WP_Error $errors
326 function network_step2( $errors = false ) {
329 $hostname = get_clean_basedomain();
330 $slashed_home = trailingslashit( get_option( 'home' ) );
331 $base = parse_url( $slashed_home, PHP_URL_PATH );
332 $document_root_fix = str_replace( '\\', '/', realpath( $_SERVER['DOCUMENT_ROOT'] ) );
333 $abspath_fix = str_replace( '\\', '/', ABSPATH );
334 $home_path = 0 === strpos( $abspath_fix, $document_root_fix ) ? $document_root_fix . $base : get_home_path();
335 $wp_siteurl_subdir = preg_replace( '#^' . preg_quote( $home_path, '#' ) . '#', '', $abspath_fix );
336 $rewrite_base = ! empty( $wp_siteurl_subdir ) ? ltrim( trailingslashit( $wp_siteurl_subdir ), '/' ) : '';
339 $location_of_wp_config = $abspath_fix;
340 if ( ! file_exists( ABSPATH . 'wp-config.php' ) && file_exists( dirname( ABSPATH ) . '/wp-config.php' ) ) {
341 $location_of_wp_config = dirname( $abspath_fix );
343 $location_of_wp_config = trailingslashit( $location_of_wp_config );
345 // Wildcard DNS message.
346 if ( is_wp_error( $errors ) )
347 echo '<div class="error">' . $errors->get_error_message() . '</div>';
350 if ( allow_subdomain_install() )
351 $subdomain_install = allow_subdirectory_install() ? ! empty( $_POST['subdomain_install'] ) : true;
353 $subdomain_install = false;
355 if ( is_multisite() ) {
356 $subdomain_install = is_subdomain_install();
358 <p><?php _e( 'The original configuration steps are shown here for reference.' ); ?></p>
361 $subdomain_install = (bool) $wpdb->get_var( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = 1 AND meta_key = 'subdomain_install'" );
363 <div class="error"><p><strong><?php _e('Warning:'); ?></strong> <?php _e( 'An existing WordPress network was detected.' ); ?></p></div>
364 <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>
369 $subdir_match = $subdomain_install ? '' : '([_0-9a-zA-Z-]+/)?';
370 $subdir_replacement_01 = $subdomain_install ? '' : '$1';
371 $subdir_replacement_12 = $subdomain_install ? '$1' : '$2';
373 if ( $_POST || ! is_multisite() ) {
375 <h3><?php esc_html_e( 'Enabling the Network' ); ?></h3>
376 <p><?php _e( 'Complete the following steps to enable the features for creating a network of sites.' ); ?></p>
377 <div class="updated inline"><p><?php
378 if ( file_exists( $home_path . '.htaccess' ) ) {
379 echo '<strong>' . __( 'Caution:' ) . '</strong> ';
381 /* translators: 1: wp-config.php 2: .htaccess */
382 __( 'We recommend you back up your existing %1$s and %2$s files.' ),
383 '<code>wp-config.php</code>',
384 '<code>.htaccess</code>'
386 } elseif ( file_exists( $home_path . 'web.config' ) ) {
387 echo '<strong>' . __( 'Caution:' ) . '</strong> ';
389 /* translators: 1: wp-config.php 2: web.config */
390 __( 'We recommend you back up your existing %1$s and %2$s files.' ),
391 '<code>wp-config.php</code>',
392 '<code>web.config</code>'
395 echo '<strong>' . __( 'Caution:' ) . '</strong> ';
397 /* translators: 1: wp-config.php */
398 __( 'We recommend you back up your existing %s file.' ),
399 '<code>wp-config.php</code>'
408 /* translators: 1: wp-config.php 2: location of wp-config file, 3: translated version of "That's all, stop editing! Happy blogging." */
409 __( 'Add the following to your %1$s file in %2$s <strong>above</strong> the line reading %3$s:' ),
410 '<code>wp-config.php</code>',
411 '<code>' . $location_of_wp_config . '</code>',
413 * translators: This string should only be translated if wp-config-sample.php is localized.
414 * You can check the localized release package or
415 * https://i18n.svn.wordpress.org/<locale code>/branches/<wp version>/dist/wp-config-sample.php
417 '<code>/* ' . __( 'That’s all, stop editing! Happy blogging.' ) . ' */</code>'
419 <textarea class="code" readonly="readonly" cols="100" rows="7">
420 define('MULTISITE', true);
421 define('SUBDOMAIN_INSTALL', <?php echo $subdomain_install ? 'true' : 'false'; ?>);
422 define('DOMAIN_CURRENT_SITE', '<?php echo $hostname; ?>');
423 define('PATH_CURRENT_SITE', '<?php echo $base; ?>');
424 define('SITE_ID_CURRENT_SITE', 1);
425 define('BLOG_ID_CURRENT_SITE', 1);
428 $keys_salts = array( 'AUTH_KEY' => '', 'SECURE_AUTH_KEY' => '', 'LOGGED_IN_KEY' => '', 'NONCE_KEY' => '', 'AUTH_SALT' => '', 'SECURE_AUTH_SALT' => '', 'LOGGED_IN_SALT' => '', 'NONCE_SALT' => '' );
429 foreach ( $keys_salts as $c => $v ) {
431 unset( $keys_salts[ $c ] );
434 if ( ! empty( $keys_salts ) ) {
435 $keys_salts_str = '';
436 $from_api = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
437 if ( is_wp_error( $from_api ) ) {
438 foreach ( $keys_salts as $c => $v ) {
439 $keys_salts_str .= "\ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
442 $from_api = explode( "\n", wp_remote_retrieve_body( $from_api ) );
443 foreach ( $keys_salts as $c => $v ) {
444 $keys_salts_str .= "\ndefine( '$c', '" . substr( array_shift( $from_api ), 28, 64 ) . "' );";
447 $num_keys_salts = count( $keys_salts );
451 if ( 1 == $num_keys_salts ) {
453 /* translators: 1: wp-config.php */
454 __( 'This unique authentication key is also missing from your %s file.' ),
455 '<code>wp-config.php</code>'
459 /* translators: 1: wp-config.php */
460 __( 'These unique authentication keys are also missing from your %s file.' ),
461 '<code>wp-config.php</code>'
465 <?php _e( 'To make your installation more secure, you should also add:' ); ?>
467 <textarea class="code" readonly="readonly" cols="100" rows="<?php echo $num_keys_salts; ?>"><?php echo esc_textarea( $keys_salts_str ); ?></textarea>
473 if ( iis7_supports_permalinks() ) :
474 // IIS doesn't support RewriteBase, all your RewriteBase are belong to us
475 $iis_subdir_match = ltrim( $base, '/' ) . $subdir_match;
476 $iis_rewrite_base = ltrim( $base, '/' ) . $rewrite_base;
477 $iis_subdir_replacement = $subdomain_install ? '' : '{R:1}';
479 $web_config_file = '<?xml version="1.0" encoding="UTF-8"?>
484 <rule name="WordPress Rule 1" stopProcessing="true">
485 <match url="^index\.php$" ignoreCase="false" />
486 <action type="None" />
488 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
489 $web_config_file .= '
490 <rule name="WordPress Rule for Files" stopProcessing="true">
491 <match url="^' . $iis_subdir_match . 'files/(.+)" ignoreCase="false" />
492 <action type="Rewrite" url="' . $iis_rewrite_base . WPINC . '/ms-files.php?file={R:1}" appendQueryString="false" />
495 $web_config_file .= '
496 <rule name="WordPress Rule 2" stopProcessing="true">
497 <match url="^' . $iis_subdir_match . 'wp-admin$" ignoreCase="false" />
498 <action type="Redirect" url="' . $iis_subdir_replacement . 'wp-admin/" redirectType="Permanent" />
500 <rule name="WordPress Rule 3" stopProcessing="true">
501 <match url="^" ignoreCase="false" />
502 <conditions logicalGrouping="MatchAny">
503 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
504 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
506 <action type="None" />
508 <rule name="WordPress Rule 4" stopProcessing="true">
509 <match url="^' . $iis_subdir_match . '(wp-(content|admin|includes).*)" ignoreCase="false" />
510 <action type="Rewrite" url="' . $iis_rewrite_base . '{R:1}" />
512 <rule name="WordPress Rule 5" stopProcessing="true">
513 <match url="^' . $iis_subdir_match . '([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
514 <action type="Rewrite" url="' . $iis_rewrite_base . '{R:2}" />
516 <rule name="WordPress Rule 6" stopProcessing="true">
517 <match url="." ignoreCase="false" />
518 <action type="Rewrite" url="index.php" />
528 /* translators: 1: a filename like .htaccess. 2: a file path. */
529 __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
530 '<code>web.config</code>',
531 '<code>' . $home_path . '</code>'
534 if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
535 echo '<p><strong>' . __('Warning:') . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
537 <textarea class="code" readonly="readonly" cols="100" rows="20"><?php echo esc_textarea( $web_config_file ); ?>
541 <?php else : // end iis7_supports_permalinks(). construct an htaccess file instead:
543 $ms_files_rewriting = '';
544 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
545 $ms_files_rewriting = "\n# uploaded files\nRewriteRule ^";
546 $ms_files_rewriting .= $subdir_match . "files/(.+) {$rewrite_base}" . WPINC . "/ms-files.php?file={$subdir_replacement_12} [L]" . "\n";
549 $htaccess_file = <<<EOF
552 RewriteRule ^index\.php$ - [L]
553 {$ms_files_rewriting}
554 # add a trailing slash to /wp-admin
555 RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement_01}wp-admin/ [R=301,L]
557 RewriteCond %{REQUEST_FILENAME} -f [OR]
558 RewriteCond %{REQUEST_FILENAME} -d
560 RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}{$subdir_replacement_12} [L]
561 RewriteRule ^{$subdir_match}(.*\.php)$ {$rewrite_base}$subdir_replacement_12 [L]
562 RewriteRule . index.php [L]
568 /* translators: 1: a filename like .htaccess. 2: a file path. */
569 __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
570 '<code>.htaccess</code>',
571 '<code>' . $home_path . '</code>'
574 if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
575 echo '<p><strong>' . __('Warning:') . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
577 <textarea class="code" readonly="readonly" cols="100" rows="<?php echo substr_count( $htaccess_file, "\n" ) + 1; ?>">
578 <?php echo esc_textarea( $htaccess_file ); ?></textarea></li>
581 <?php endif; // end IIS/Apache code branches.
583 if ( !is_multisite() ) { ?>
584 <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>