wp_install()
云策文档标注
概述
wp_install() 是 WordPress 的核心函数,用于执行网站的完整安装过程,包括数据库设置、用户创建和初始选项配置。它处理从环境检查到最终通知的多个步骤,确保站点正确初始化。
关键要点
- 函数 wp_install() 负责安装 WordPress 站点,设置数据库、创建管理员用户和初始化选项。
- 参数包括必填的站点标题、用户名、邮箱和公开性,以及可选的密码和语言设置。
- 返回一个数组,包含新安装站点的 URL、用户 ID、密码和密码消息。
- 内部执行数据库版本检查、缓存刷新、角色和选项填充、用户管理、计划事件调整等操作。
- 触发 wp_install Hook,允许开发者在安装完成后执行自定义操作。
代码示例
function wp_install(
$blog_title,
$user_name,
$user_email,
$is_public,
$deprecated = '',
#[SensitiveParameter]
$user_password = '',
$language = ''
) {
// 函数体代码(此处省略以保持简洁,实际文档中包含完整实现)
}注意事项
- 参数 $deprecated 自 WordPress 2.6.0 起已弃用,使用时需注意兼容性。
- 函数内部使用 wp_unschedule_hook() 延迟更新检查,避免安装后立即进入维护模式。
- 如果用户已存在,则继承其密码;否则根据是否提供密码生成随机密码或使用指定密码。
- 安装完成后会触发 wp_install Hook,开发者可以利用此 Hook 进行扩展。
原文内容
Installs the site.
Description
Runs the required functions to set up and populate the database, including primary admin user and initial options.
Parameters
$blog_titlestringrequired-
Site title.
$user_namestringrequired-
User’s username.
$user_emailstringrequired-
User’s email.
$is_publicboolrequired-
Whether the site is public.
$deprecatedstringoptional-
Not used.
$user_passwordstringoptional-
User’s chosen password. Default empty (random password).
$languagestringoptional-
Language chosen. Default empty.
Source
function wp_install(
$blog_title,
$user_name,
$user_email,
$is_public,
$deprecated = '',
#[SensitiveParameter]
$user_password = '',
$language = ''
) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.6.0' );
}
wp_check_mysql_version();
wp_cache_flush();
make_db_current_silent();
/*
* Ensure update checks are delayed after installation.
*
* This prevents users being presented with a maintenance mode screen
* immediately after installation.
*/
wp_unschedule_hook( 'wp_version_check' );
wp_unschedule_hook( 'wp_update_plugins' );
wp_unschedule_hook( 'wp_update_themes' );
wp_schedule_event( time() + HOUR_IN_SECONDS, 'twicedaily', 'wp_version_check' );
wp_schedule_event( time() + ( 1.5 * HOUR_IN_SECONDS ), 'twicedaily', 'wp_update_plugins' );
wp_schedule_event( time() + ( 2 * HOUR_IN_SECONDS ), 'twicedaily', 'wp_update_themes' );
populate_options();
populate_roles();
update_option( 'blogname', $blog_title );
update_option( 'admin_email', $user_email );
update_option( 'blog_public', $is_public );
// Freshness of site - in the future, this could get more specific about actions taken, perhaps.
update_option( 'fresh_site', 1, false );
if ( $language ) {
update_option( 'WPLANG', $language );
}
$guessurl = wp_guess_url();
update_option( 'siteurl', $guessurl );
// If not a public site, don't ping.
if ( ! $is_public ) {
update_option( 'default_pingback_flag', 0 );
}
/*
* Create default user. If the user already exists, the user tables are
* being shared among sites. Just set the role in that case.
*/
$user_id = username_exists( $user_name );
$user_password = trim( $user_password );
$email_password = false;
$user_created = false;
if ( ! $user_id && empty( $user_password ) ) {
$user_password = wp_generate_password( 12, false );
$message = __( '<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.' );
$user_id = wp_create_user( $user_name, $user_password, $user_email );
update_user_meta( $user_id, 'default_password_nag', true );
$email_password = true;
$user_created = true;
} elseif ( ! $user_id ) {
// Password has been provided.
$message = '<em>' . __( 'Your chosen password.' ) . '</em>';
$user_id = wp_create_user( $user_name, $user_password, $user_email );
$user_created = true;
} else {
$message = __( 'User already exists. Password inherited.' );
}
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
if ( $user_created ) {
$user->user_url = $guessurl;
wp_update_user( $user );
}
wp_install_defaults( $user_id );
wp_install_maybe_enable_pretty_permalinks();
flush_rewrite_rules();
wp_new_blog_notification( $blog_title, $guessurl, $user_id, ( $email_password ? $user_password : __( 'The password you chose during installation.' ) ) );
wp_cache_flush();
/**
* Fires after a site is fully installed.
*
* @since 3.9.0
*
* @param WP_User $user The site owner.
*/
do_action( 'wp_install', $user );
return array(
'url' => $guessurl,
'user_id' => $user_id,
'password' => $user_password,
'password_message' => $message,
);
}
Hooks
- do_action( ‘wp_install’, WP_User $user )
-
Fires after a site is fully installed.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |