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 * Filter whether to enable the subdirectory install feature in Multisite.
59 * @param bool true 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 'http://httpd.apache.org/docs/mod/mod_rewrite.html',
184 'http://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' ) ) {
380 /* translators: 1: wp-config.php 2: .htaccess */
381 __( '<strong>Caution:</strong> We recommend you back up your existing %1$s and %2$s files.' ),
382 '<code>wp-config.php</code>',
383 '<code>.htaccess</code>'
385 } elseif ( file_exists( $home_path . 'web.config' ) ) {
387 /* translators: 1: wp-config.php 2: web.config */
388 __( '<strong>Caution:</strong> We recommend you back up your existing %1$s and %2$s files.' ),
389 '<code>wp-config.php</code>',
390 '<code>web.config</code>'
394 /* translators: 1: wp-config.php */
395 __( '<strong>Caution:</strong> We recommend you back up your existing %s file.' ),
396 '<code>wp-config.php</code>'
405 /* translators: 1: wp-config.php 2: location of wp-config file */
406 __( 'Add the following to your %1$s file in %2$s <strong>above</strong> the line reading <code>/* That’s all, stop editing! Happy blogging. */</code>:' ),
407 '<code>wp-config.php</code>',
408 '<code>' . $location_of_wp_config . '</code>'
410 <textarea class="code" readonly="readonly" cols="100" rows="7">
411 define('MULTISITE', true);
412 define('SUBDOMAIN_INSTALL', <?php echo $subdomain_install ? 'true' : 'false'; ?>);
413 define('DOMAIN_CURRENT_SITE', '<?php echo $hostname; ?>');
414 define('PATH_CURRENT_SITE', '<?php echo $base; ?>');
415 define('SITE_ID_CURRENT_SITE', 1);
416 define('BLOG_ID_CURRENT_SITE', 1);
419 $keys_salts = array( 'AUTH_KEY' => '', 'SECURE_AUTH_KEY' => '', 'LOGGED_IN_KEY' => '', 'NONCE_KEY' => '', 'AUTH_SALT' => '', 'SECURE_AUTH_SALT' => '', 'LOGGED_IN_SALT' => '', 'NONCE_SALT' => '' );
420 foreach ( $keys_salts as $c => $v ) {
422 unset( $keys_salts[ $c ] );
425 if ( ! empty( $keys_salts ) ) {
426 $keys_salts_str = '';
427 $from_api = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
428 if ( is_wp_error( $from_api ) ) {
429 foreach ( $keys_salts as $c => $v ) {
430 $keys_salts_str .= "\ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
433 $from_api = explode( "\n", wp_remote_retrieve_body( $from_api ) );
434 foreach ( $keys_salts as $c => $v ) {
435 $keys_salts_str .= "\ndefine( '$c', '" . substr( array_shift( $from_api ), 28, 64 ) . "' );";
438 $num_keys_salts = count( $keys_salts );
442 if ( 1 == $num_keys_salts ) {
444 /* translators: 1: wp-config.php */
445 __( 'This unique authentication key is also missing from your %s file.' ),
446 '<code>wp-config.php</code>'
450 /* translators: 1: wp-config.php */
451 __( 'These unique authentication keys are also missing from your %s file.' ),
452 '<code>wp-config.php</code>'
456 <?php _e( 'To make your installation more secure, you should also add:' ); ?>
458 <textarea class="code" readonly="readonly" cols="100" rows="<?php echo $num_keys_salts; ?>"><?php echo esc_textarea( $keys_salts_str ); ?></textarea>
464 if ( iis7_supports_permalinks() ) :
465 // IIS doesn't support RewriteBase, all your RewriteBase are belong to us
466 $iis_subdir_match = ltrim( $base, '/' ) . $subdir_match;
467 $iis_rewrite_base = ltrim( $base, '/' ) . $rewrite_base;
468 $iis_subdir_replacement = $subdomain_install ? '' : '{R:1}';
470 $web_config_file = '<?xml version="1.0" encoding="UTF-8"?>
475 <rule name="WordPress Rule 1" stopProcessing="true">
476 <match url="^index\.php$" ignoreCase="false" />
477 <action type="None" />
479 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
480 $web_config_file .= '
481 <rule name="WordPress Rule for Files" stopProcessing="true">
482 <match url="^' . $iis_subdir_match . 'files/(.+)" ignoreCase="false" />
483 <action type="Rewrite" url="' . $iis_rewrite_base . WPINC . '/ms-files.php?file={R:1}" appendQueryString="false" />
486 $web_config_file .= '
487 <rule name="WordPress Rule 2" stopProcessing="true">
488 <match url="^' . $iis_subdir_match . 'wp-admin$" ignoreCase="false" />
489 <action type="Redirect" url="' . $iis_subdir_replacement . 'wp-admin/" redirectType="Permanent" />
491 <rule name="WordPress Rule 3" stopProcessing="true">
492 <match url="^" ignoreCase="false" />
493 <conditions logicalGrouping="MatchAny">
494 <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" />
495 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" />
497 <action type="None" />
499 <rule name="WordPress Rule 4" stopProcessing="true">
500 <match url="^' . $iis_subdir_match . '(wp-(content|admin|includes).*)" ignoreCase="false" />
501 <action type="Rewrite" url="' . $iis_rewrite_base . '{R:1}" />
503 <rule name="WordPress Rule 5" stopProcessing="true">
504 <match url="^' . $iis_subdir_match . '([_0-9a-zA-Z-]+/)?(.*\.php)$" ignoreCase="false" />
505 <action type="Rewrite" url="' . $iis_rewrite_base . '{R:2}" />
507 <rule name="WordPress Rule 6" stopProcessing="true">
508 <match url="." ignoreCase="false" />
509 <action type="Rewrite" url="index.php" />
519 /* translators: 1: a filename like .htaccess. 2: a file path. */
520 __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
521 '<code>web.config</code>',
522 '<code>' . $home_path . '</code>'
525 if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
526 echo '<p><strong>' . __('Warning:') . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
528 <textarea class="code" readonly="readonly" cols="100" rows="20"><?php echo esc_textarea( $web_config_file ); ?>
532 <?php else : // end iis7_supports_permalinks(). construct an htaccess file instead:
534 $ms_files_rewriting = '';
535 if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
536 $ms_files_rewriting = "\n# uploaded files\nRewriteRule ^";
537 $ms_files_rewriting .= $subdir_match . "files/(.+) {$rewrite_base}" . WPINC . "/ms-files.php?file={$subdir_replacement_12} [L]" . "\n";
540 $htaccess_file = <<<EOF
543 RewriteRule ^index\.php$ - [L]
544 {$ms_files_rewriting}
545 # add a trailing slash to /wp-admin
546 RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement_01}wp-admin/ [R=301,L]
548 RewriteCond %{REQUEST_FILENAME} -f [OR]
549 RewriteCond %{REQUEST_FILENAME} -d
551 RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}{$subdir_replacement_12} [L]
552 RewriteRule ^{$subdir_match}(.*\.php)$ {$rewrite_base}$subdir_replacement_12 [L]
553 RewriteRule . index.php [L]
559 /* translators: 1: a filename like .htaccess. 2: a file path. */
560 __( 'Add the following to your %1$s file in %2$s, <strong>replacing</strong> other WordPress rules:' ),
561 '<code>.htaccess</code>',
562 '<code>' . $home_path . '</code>'
565 if ( ! $subdomain_install && WP_CONTENT_DIR != ABSPATH . 'wp-content' )
566 echo '<p><strong>' . __('Warning:') . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '</strong></p>';
568 <textarea class="code" readonly="readonly" cols="100" rows="<?php echo substr_count( $htaccess_file, "\n" ) + 1; ?>">
569 <?php echo esc_textarea( $htaccess_file ); ?></textarea></li>
572 <?php endif; // end IIS/Apache code branches.
574 if ( !is_multisite() ) { ?>
575 <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>