network_step2()
概述
network_step2() 函数用于在 WordPress 网络安装过程中输出第二步的配置界面,处理子域名或子目录安装选项,并生成 wp-config.php 和服务器配置文件(如 .htaccess 或 web.config)的代码片段。
关键要点
- 函数接受一个可选参数 $errors,用于传递 WP_Error 对象以显示错误消息。
- 自动检测服务器环境(如 Nginx 或 Apache/IIS)并生成相应的重写规则。
- 根据用户选择(子域名或子目录安装)动态生成 MULTISITE 相关常量定义。
- 提供安全密钥生成逻辑,支持从 WordPress API 获取或本地生成。
- 包含文件备份警告和配置步骤的详细说明,适用于多站点网络安装。
代码示例
define( 'MULTISITE', true );
define( 'SUBDOMAIN_INSTALL', false );
define( 'DOMAIN_CURRENT_SITE', 'example.com' );
define( 'PATH_CURRENT_SITE', '/' );
define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );注意事项
- 在运行此步骤前,需确保已正确设置 wp-config.php 文件路径和服务器文档根目录。
- 对于子目录安装,如果使用了自定义 wp-content 目录,可能存在兼容性问题。
- 函数内部调用了多个辅助函数,如 get_home_path()、allow_subdomain_install() 等,需确保这些函数可用。
Prints step 2 for Network installation process.
Parameters
$errorsfalse|WP_Erroroptional-
Error object.
Default:
false
Source
function network_step2( $errors = false ) {
global $wpdb, $is_nginx;
$hostname = get_clean_basedomain();
$slashed_home = trailingslashit( get_option( 'home' ) );
$base = parse_url( $slashed_home, PHP_URL_PATH );
$document_root_fix = str_replace( '\', '/', realpath( $_SERVER['DOCUMENT_ROOT'] ) );
$abspath_fix = str_replace( '\', '/', ABSPATH );
$home_path = str_starts_with( $abspath_fix, $document_root_fix ) ? $document_root_fix . $base : get_home_path();
$wp_siteurl_subdir = preg_replace( '#^' . preg_quote( $home_path, '#' ) . '#', '', $abspath_fix );
$rewrite_base = ! empty( $wp_siteurl_subdir ) ? ltrim( trailingslashit( $wp_siteurl_subdir ), '/' ) : '';
$location_of_wp_config = $abspath_fix;
if ( ! file_exists( ABSPATH . 'wp-config.php' ) && file_exists( dirname( ABSPATH ) . '/wp-config.php' ) ) {
$location_of_wp_config = dirname( $abspath_fix );
}
$location_of_wp_config = trailingslashit( $location_of_wp_config );
// Wildcard DNS message.
if ( is_wp_error( $errors ) ) {
wp_admin_notice(
$errors->get_error_message(),
array(
'additional_classes' => array( 'error' ),
)
);
}
if ( $_POST ) {
if ( allow_subdomain_install() ) {
$subdomain_install = allow_subdirectory_install() ? ! empty( $_POST['subdomain_install'] ) : true;
} else {
$subdomain_install = false;
}
} else {
if ( is_multisite() ) {
$subdomain_install = is_subdomain_install();
?>
<p></p>
get_var( "SELECT meta_value FROM $wpdb->sitemeta WHERE site_id = 1 AND meta_key = 'subdomain_install'" );
wp_admin_notice(
'<strong>' . __( 'Warning:' ) . '</strong> ' . __( 'An existing WordPress network was detected.' ),
array(
'additional_classes' => array( 'error' ),
)
);
?>
<p></p>
<h3></h3>
<p></p>
' . __( 'Caution:' ) . '</strong> ';
$notice_args = array(
'type' => 'warning',
'additional_classes' => array( 'inline' ),
);
if ( file_exists( $home_path . '.htaccess' ) ) {
$notice_message .= sprintf(
/* translators: 1: wp-config.php, 2: .htaccess */
__( 'You should back up your existing %1$s and %2$s files.' ),
'<code>wp-config.php',
'.htaccess'
);
} elseif ( file_exists( $home_path . 'web.config' ) ) {
$notice_message .= sprintf(
/* translators: 1: wp-config.php, 2: web.config */
__( 'You should back up your existing %1$s and %2$s files.' ),
'wp-config.php',
'web.config'
);
} else {
$notice_message .= sprintf(
/* translators: %s: wp-config.php */
__( 'You should back up your existing %s file.' ),
'wp-config.php'
);
}
wp_admin_notice( $notice_message, $notice_args );
}
?>
above the line reading %3$s:' ),
'wp-config.php',
'' . $location_of_wp_config . '',
/*
* translators: This string should only be translated if wp-config-sample.php is localized.
* You can check the localized release package or
* https://i18n.svn.wordpress.org//branches//dist/wp-config-sample.php
*/
'/* ' . __( 'That’s all, stop editing! Happy publishing.' ) . ' */'
);
?>
'',
'SECURE_AUTH_KEY' => '',
'LOGGED_IN_KEY' => '',
'NONCE_KEY' => '',
'AUTH_SALT' => '',
'SECURE_AUTH_SALT' => '',
'LOGGED_IN_SALT' => '',
'NONCE_SALT' => '',
);
foreach ( $keys_salts as $c => $v ) {
if ( defined( $c ) ) {
unset( $keys_salts[ $c ] );
}
}
if ( ! empty( $keys_salts ) ) {
$keys_salts_str = '';
$from_api = wp_remote_get( 'https://api.wordpress.org/secret-key/1.1/salt/' );
if ( is_wp_error( $from_api ) ) {
foreach ( $keys_salts as $c => $v ) {
$keys_salts_str .= "ndefine( '$c', '" . wp_generate_password( 64, true, true ) . "' );";
}
} else {
$from_api = explode( "n", wp_remote_retrieve_body( $from_api ) );
foreach ( $keys_salts as $c => $v ) {
$keys_salts_str .= "ndefine( '$c', '" . substr( array_shift( $from_api ), 28, 64 ) . "' );";
}
}
$num_keys_salts = count( $keys_salts );
?>
wp-config.php'
);
} else {
printf(
/* translators: %s: wp-config.php */
__( 'These unique authentication keys are also missing from your %s file.' ),
'wp-config.php'
);
}
?>
<textarea id="network-wpconfig-authentication" class="code" readonly="readonly" cols="100" rows="" aria-describedby="network-wpconfig-authentication-description">
';
if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
$web_config_file .= '
';
}
$web_config_file .= '
';
echo '';
printf(
/* translators: 1: File name (.htaccess or web.config), 2: File path. */
__( 'Add the following to your %1$s file in %2$s, replacing other WordPress rules:' ),
'web.config',
'' . $home_path . ''
);
echo '
';
if ( ! $subdomain_install && WP_CONTENT_DIR !== ABSPATH . 'wp-content' ) {
echo '' . __( 'Warning:' ) . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '
';
}
?>
';
printf(
/* translators: %s: Documentation URL. */
__( 'It seems your network is running with Nginx web server. Learn more about further configuration.' ),
__( 'https://developer.wordpress.org/advanced-administration/server/web-server/nginx/' )
);
echo '
';
else : // End $is_nginx. Construct an .htaccess file instead:
$ms_files_rewriting = '';
if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
$ms_files_rewriting = "n# uploaded filesnRewriteRule ^";
$ms_files_rewriting .= $subdir_match . "files/(.+) {$rewrite_base}" . WPINC . "/ms-files.php?file={$subdir_replacement_12} [L]" . "n";
}
$htaccess_file = <<<EOF
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase {$base}
RewriteRule ^index.php$ - [L]
{$ms_files_rewriting}
# add a trailing slash to /wp-admin
RewriteRule ^{$subdir_match}wp-admin$ {$subdir_replacement_01}wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^{$subdir_match}(wp-(content|admin|includes).*) {$rewrite_base}{$subdir_replacement_12} [L]
RewriteRule ^{$subdir_match}(.*.php)$ {$rewrite_base}$subdir_replacement_12 [L]
RewriteRule . index.php [L]
EOF;
echo '';
printf(
/* translators: 1: File name (.htaccess or web.config), 2: File path. */
__( 'Add the following to your %1$s file in %2$s, replacing other WordPress rules:' ),
'.htaccess',
'' . $home_path . ''
);
echo '
' . __( 'Warning:' ) . ' ' . __( 'Subdirectory networks may not be fully compatible with custom wp-content directories.' ) . '
'; } ?> <textarea id="network-htaccess-rules" class="code" readonly="readonly" cols="100" rows="" aria-describedby="network-htaccess-rules-description"><a href="">
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |