wpmu_welcome_notification()
云策文档标注
概述
wpmu_welcome_notification() 是 WordPress 多站点功能中的一个函数,用于在站点激活成功后向站点管理员发送欢迎通知邮件。该函数支持通过过滤器进行自定义或禁用。
关键要点
- 函数用途:发送站点激活成功的通知邮件给站点管理员。
- 过滤器支持:可通过 'wpmu_welcome_notification' 过滤器禁用邮件发送,通过 'update_welcome_email' 和 'update_welcome_subject' 过滤器修改邮件内容和主题。
- 参数:包括 $blog_id(站点ID)、$user_id(用户ID)、$password(用户密码)、$title(站点标题)和 $meta(注册元数据)。
- 返回值:返回布尔值,表示邮件是否成功发送。
- 内部处理:函数会获取网络设置、用户数据,并处理邮件内容的占位符替换。
代码示例
// 示例:使用过滤器修改欢迎邮件内容
add_filter('update_welcome_email', function($welcome_email, $blog_id, $user_id, $password, $title, $meta) {
// 自定义邮件内容
$custom_email = "欢迎您的新站点!站点标题:" . $title;
return $custom_email;
}, 10, 6);注意事项
- 邮件发送依赖于 wp_mail() 函数,需确保服务器邮件配置正确。
- 密码参数可能为 "N/A",表示用户账户不是新创建的。
- 函数内部会切换用户区域设置以支持本地化,并在邮件发送后恢复。
原文内容
Notifies the site administrator that their site activation was successful.
Description
Filter ‘wpmu_welcome_notification’ to disable or bypass.
Filter ‘update_welcome_email’ and ‘update_welcome_subject’ to modify the content and subject line of the notification email.
Parameters
$blog_idintrequired-
Site ID.
$user_idintrequired-
User ID.
$passwordstringrequired-
User password, or “N/A” if the user account is not new.
$titlestringrequired-
Site title.
$metaarrayoptional-
Signup meta data. By default, contains the requested privacy setting and lang_id.
Default:
array()
Source
function wpmu_welcome_notification(
$blog_id,
$user_id,
#[SensitiveParameter]
$password,
$title,
$meta = array()
) {
$current_network = get_network();
/**
* Filters whether to bypass the welcome email sent to the site administrator after site activation.
*
* Returning false disables the welcome email.
*
* @since MU (3.0.0)
*
* @param int|false $blog_id Site ID, or false to prevent the email from sending.
* @param int $user_id User ID of the site administrator.
* @param string $password User password, or "N/A" if the user account is not new.
* @param string $title Site title.
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
*/
if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) ) {
return false;
}
$user = get_userdata( $user_id );
$switched_locale = switch_to_user_locale( $user_id );
$welcome_email = get_site_option( 'welcome_email' );
if ( ! $welcome_email ) {
/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
$welcome_email = __(
'Howdy USERNAME,
Your new SITE_NAME site has been successfully set up at:
BLOG_URL
You can log in to the administrator account with the following information:
Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLwp-login.php
We hope you enjoy your new site. Thanks!
--The Team @ SITE_NAME'
);
}
$url = get_blogaddress_by_id( $blog_id );
$welcome_email = str_replace( 'SITE_NAME', $current_network->site_name, $welcome_email );
$welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
$welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
$welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
$welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
/**
* Filters the content of the welcome email sent to the site administrator after site activation.
*
* Content should be formatted for transmission via wp_mail().
*
* @since MU (3.0.0)
*
* @param string $welcome_email Message body of the email.
* @param int $blog_id Site ID.
* @param int $user_id User ID of the site administrator.
* @param string $password User password, or "N/A" if the user account is not new.
* @param string $title Site title.
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
*/
$welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
$admin_email = get_site_option( 'admin_email' );
if ( '' === $admin_email ) {
$admin_email = 'support@' . wp_parse_url( network_home_url(), PHP_URL_HOST );
}
$from_name = ( '' !== get_site_option( 'site_name' ) ) ? esc_html( get_site_option( 'site_name' ) ) : 'WordPress';
$message_headers = "From: "{$from_name}" <{$admin_email}>n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . ""n";
$message = $welcome_email;
if ( empty( $current_network->site_name ) ) {
$current_network->site_name = 'WordPress';
}
/* translators: New site notification email subject. 1: Network title, 2: New site title. */
$subject = __( 'New %1$s Site: %2$s' );
/**
* Filters the subject of the welcome email sent to the site administrator after site activation.
*
* @since MU (3.0.0)
*
* @param string $subject Subject of the email.
*/
$subject = apply_filters( 'update_welcome_subject', sprintf( $subject, $current_network->site_name, wp_unslash( $title ) ) );
wp_mail( $user->user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
if ( $switched_locale ) {
restore_previous_locale();
}
return true;
}
Hooks
- apply_filters( ‘update_welcome_email’, string $welcome_email, int $blog_id, int $user_id, string $password, string $title, array $meta )
-
Filters the content of the welcome email sent to the site administrator after site activation.
- apply_filters( ‘update_welcome_subject’, string $subject )
-
Filters the subject of the welcome email sent to the site administrator after site activation.
- apply_filters( ‘wpmu_welcome_notification’, int|false $blog_id, int $user_id, string $password, string $title, array $meta )
-
Filters whether to bypass the welcome email sent to the site administrator after site activation.
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |