wp_new_blog_notification()
云策文档标注
概述
wp_new_blog_notification() 函数用于在 WordPress 安装完成后,向站点管理员发送通知邮件,包含登录凭据信息。
关键要点
- 函数功能:发送安装完成通知邮件给新管理员,记录登录凭据。
- 参数:$blog_title(站点标题)、$blog_url(站点URL)、$user_id(管理员用户ID)、$password(管理员密码,通常传递占位符消息而非实际密码)。
- 内部流程:通过 WP_User 获取用户信息,构建邮件内容,使用 wp_mail() 发送。
- Hook:提供 wp_installed_email 过滤器,允许自定义邮件内容。
- 相关函数:涉及 WP_User、wp_mail()、wp_login_url()、__() 和 apply_filters() 等。
代码示例
function wp_new_blog_notification(
$blog_title,
$blog_url,
$user_id,
#[SensitiveParameter]
$password
) {
$user = new WP_User( $user_id );
$email = $user->user_email;
$name = $user->user_login;
$login_url = wp_login_url();
$message = sprintf(
__(
'Your new WordPress site has been successfully set up at:nn%1$snnYou can log in to the administrator account with the following information:nnUsername: %2$snPassword: %3$snLog in here: %4$snnWe hope you enjoy your new site. Thanks!nn--The WordPress TeamnHome'
),
$blog_url,
$name,
$password,
$login_url
);
$installed_email = array(
'to' => $email,
'subject' => __( 'New WordPress Site' ),
'message' => $message,
'headers' => '',
);
$installed_email = apply_filters( 'wp_installed_email', $installed_email, $user, $blog_title, $blog_url, $password );
wp_mail(
$installed_email['to'],
$installed_email['subject'],
$installed_email['message'],
$installed_email['headers']
);
}注意事项
- 密码参数通常传递占位符消息而非实际密码,以增强安全性。
- 邮件内容可通过 wp_installed_email 过滤器进行自定义,适用于多语言或特定格式需求。
- 函数自 WordPress 2.1.0 版本引入,主要用于 wp_install() 过程中。
原文内容
Notifies the site admin that the installation of WordPress is complete.
Description
Sends an email to the new administrator that the installation is complete and provides them with a record of their login credentials.
Parameters
$blog_titlestringrequired-
Site title.
$blog_urlstringrequired-
Site URL.
$user_idintrequired-
Administrator’s user ID.
$passwordstringrequired-
Administrator’s password. Note that a placeholder message is usually passed instead of the actual password.
Source
function wp_new_blog_notification(
$blog_title,
$blog_url,
$user_id,
#[SensitiveParameter]
$password
) {
$user = new WP_User( $user_id );
$email = $user->user_email;
$name = $user->user_login;
$login_url = wp_login_url();
$message = sprintf(
/* translators: New site notification email. 1: New site URL, 2: User login, 3: User password or password reset link, 4: Login URL. */
__(
'Your new WordPress site has been successfully set up at:
%1$s
You can log in to the administrator account with the following information:
Username: %2$s
Password: %3$s
Log in here: %4$s
We hope you enjoy your new site. Thanks!
--The WordPress Team
<blockquote class="wp-embedded-content" data-secret="XRituu1tJe"><a href="https://wordpress.org/">Home</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" title="“Home” — WordPress.org" src="https://wordpress.org/embed/#?secret=YJlJBvpEMC#?secret=XRituu1tJe" data-secret="XRituu1tJe" width="500" height="282" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
'
),
$blog_url,
$name,
$password,
$login_url
);
$installed_email = array(
'to' => $email,
'subject' => __( 'New WordPress Site' ),
'message' => $message,
'headers' => '',
);
/**
* Filters the contents of the email sent to the site administrator when WordPress is installed.
*
* @since 5.6.0
*
* @param array $installed_email {
* Used to build wp_mail().
*
* @type string $to The email address of the recipient.
* @type string $subject The subject of the email.
* @type string $message The content of the email.
* @type string $headers Headers.
* }
* @param WP_User $user The site administrator user object.
* @param string $blog_title The site title.
* @param string $blog_url The site URL.
* @param string $password The site administrator's password. Note that a placeholder message
* is usually passed instead of the user's actual password.
*/
$installed_email = apply_filters( 'wp_installed_email', $installed_email, $user, $blog_title, $blog_url, $password );
wp_mail(
$installed_email['to'],
$installed_email['subject'],
$installed_email['message'],
$installed_email['headers']
);
}
Hooks
- apply_filters( ‘wp_installed_email’, array $installed_email, WP_User $user, string $blog_title, string $blog_url, string $password )
-
Filters the contents of the email sent to the site administrator when WordPress is installed.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |