wpmu_signup_blog_notification()
概述
wpmu_signup_blog_notification() 是 WordPress 多站点功能中用于发送新站点注册确认邮件的核心函数。当用户注册新站点时,该函数会生成并发送包含激活链接的邮件,站点需点击链接激活后才能生效。
关键要点
- 函数在站点注册启用时被调用,发送确认邮件,邮件内容包含激活链接。
- 通过过滤器 'wpmu_signup_blog_notification' 可绕过或替换默认通知行为。
- 过滤器 'wpmu_signup_blog_notification_email' 和 'wpmu_signup_blog_notification_subject' 允许自定义邮件内容和主题。
- 函数接受多个参数,包括站点域名、路径、标题、用户登录名、邮箱、激活键和元数据,返回布尔值表示发送成功与否。
- 内部使用 wp_mail() 发送邮件,并处理本地化切换以适配用户语言环境。
代码示例
function wpmu_signup_blog_notification(
$domain,
$path,
$title,
$user_login,
$user_email,
#[SensitiveParameter]
$key,
$meta = array()
) {
// 过滤器检查是否绕过邮件发送
if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user_login, $user_email, $key, $meta ) ) {
return false;
}
// 生成激活链接和邮件内容
$activate_url = network_site_url( "wp-activate.php?key=$key" );
$message = apply_filters( 'wpmu_signup_blog_notification_email', __( "To activate your site, please click the following link:\n\n%1\$s\n\nAfter you activate, you will receive *another email* with your login." ), $domain, $path, $title, $user_login, $user_email, $key, $meta );
$subject = apply_filters( 'wpmu_signup_blog_notification_subject', _x( '[%1$s] Activate %2$s', 'New site notification email subject' ), $domain, $path, $title, $user_login, $user_email, $key, $meta );
wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
return true;
}注意事项
- 函数依赖于多站点环境,仅在站点注册功能启用时有效。
- 激活链接的生成逻辑会根据是否为子域名安装或网络ID进行调整。
- 邮件发送前会切换用户本地化设置,发送后恢复,以确保内容语言正确。
- 开发者应谨慎使用过滤器,避免破坏默认邮件流程或引入安全风险。
Sends a confirmation request email to a user when they sign up for a new site. The new site will not become active until the confirmation link is clicked.
Description
This is the notification function used when site registration is enabled.
Filter ‘wpmu_signup_blog_notification’ to bypass this function or replace it with your own notification behavior.
Filter ‘wpmu_signup_blog_notification_email’ and ‘wpmu_signup_blog_notification_subject’ to change the content and subject line of the email sent to newly registered users.
Parameters
$domainstringrequired-
The new blog domain.
$pathstringrequired-
The new blog path.
$titlestringrequired-
The site title.
$user_loginstringrequired-
The user’s login name.
$user_emailstringrequired-
The user’s email address.
$keystringrequired-
The activation key created in wpmu_signup_blog() .
$metaarrayoptional-
Signup meta data. By default, contains the requested privacy setting and lang_id.
Default:
array()
Source
function wpmu_signup_blog_notification(
$domain,
$path,
$title,
$user_login,
$user_email,
#[SensitiveParameter]
$key,
$meta = array()
) {
/**
* Filters whether to bypass the new site email notification.
*
* @since MU (3.0.0)
*
* @param string|false $domain Site domain, or false to prevent the email from sending.
* @param string $path Site path.
* @param string $title Site title.
* @param string $user_login User login name.
* @param string $user_email User email address.
* @param string $key Activation key created in wpmu_signup_blog().
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
*/
if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user_login, $user_email, $key, $meta ) ) {
return false;
}
// Send email with activation link.
if ( ! is_subdomain_install() || get_current_network_id() !== 1 ) {
$activate_url = network_site_url( "wp-activate.php?key=$key" );
} else {
$activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo Use *_url() API.
}
$activate_url = esc_url( $activate_url );
$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";
$user = get_user_by( 'login', $user_login );
$switched_locale = $user && switch_to_user_locale( $user->ID );
$message = sprintf(
/**
* Filters the message content of the new blog notification email.
*
* Content should be formatted for transmission via wp_mail().
*
* @since MU (3.0.0)
*
* @param string $content Content of the notification email.
* @param string $domain Site domain.
* @param string $path Site path.
* @param string $title Site title.
* @param string $user_login User login name.
* @param string $user_email User email address.
* @param string $key Activation key created in wpmu_signup_blog().
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
*/
apply_filters(
'wpmu_signup_blog_notification_email',
/* translators: New site notification email. 1: Activation URL, 2: New site URL. */
__( "To activate your site, please click the following link:nn%1$snnAfter you activate, you will receive *another email* with your login.nnAfter you activate, you can visit your site here:nn%2$s" ),
$domain,
$path,
$title,
$user_login,
$user_email,
$key,
$meta
),
$activate_url,
esc_url( "http://{$domain}{$path}" ),
$key
);
$subject = sprintf(
/**
* Filters the subject of the new blog notification email.
*
* @since MU (3.0.0)
*
* @param string $subject Subject of the notification email.
* @param string $domain Site domain.
* @param string $path Site path.
* @param string $title Site title.
* @param string $user_login User login name.
* @param string $user_email User email address.
* @param string $key Activation key created in wpmu_signup_blog().
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
*/
apply_filters(
'wpmu_signup_blog_notification_subject',
/* translators: New site notification email subject. 1: Network title, 2: New site URL. */
_x( '[%1$s] Activate %2$s', 'New site notification email subject' ),
$domain,
$path,
$title,
$user_login,
$user_email,
$key,
$meta
),
$from_name,
esc_url( 'http://' . $domain . $path )
);
wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
if ( $switched_locale ) {
restore_previous_locale();
}
return true;
}
Hooks
- apply_filters( ‘wpmu_signup_blog_notification’, string|false $domain, string $path, string $title, string $user_login, string $user_email, string $key, array $meta )
-
Filters whether to bypass the new site email notification.
- apply_filters( ‘wpmu_signup_blog_notification_email’, string $content, string $domain, string $path, string $title, string $user_login, string $user_email, string $key, array $meta )
-
Filters the message content of the new blog notification email.
- apply_filters( ‘wpmu_signup_blog_notification_subject’, string $subject, string $domain, string $path, string $title, string $user_login, string $user_email, string $key, array $meta )
-
Filters the subject of the new blog notification email.
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |