函数文档

wpmu_signup_user_notification()

💡 云策文档标注

概述

wpmu_signup_user_notification() 是 WordPress 多站点功能中用于发送新用户注册确认邮件的函数,适用于用户仅注册账户而未同时注册站点的情况。该函数通过邮件发送激活链接,用户需点击链接以激活账户。

关键要点

  • 函数在用户注册新账户(不注册站点)时触发,发送包含激活链接的确认邮件。
  • 用户账户在点击确认链接前不会激活,确保账户验证流程。
  • 提供多个过滤器(Filter)以自定义通知行为、邮件内容和主题,例如 'wpmu_signup_user_notification' 可绕过或替换整个通知函数。
  • 参数包括 $user_login(用户名)、$user_email(邮箱)、$key(激活密钥)和 $meta(元数据数组),其中 $key 由 wpmu_signup_user() 生成。
  • 函数返回布尔值,成功发送邮件返回 true,否则返回 false。
  • 内部使用 wp_mail() 发送邮件,并处理本地化切换以支持多语言环境。

代码示例

function wpmu_signup_user_notification(
    $user_login,
    $user_email,
    #[SensitiveParameter]
    $key,
    $meta = array()
) {
    // 过滤器示例:跳过通知
    if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) {
        return false;
    }
    // 邮件发送逻辑
    $message = apply_filters( 'wpmu_signup_user_notification_email', __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ), $user_login, $user_email, $key, $meta );
    $subject = apply_filters( 'wpmu_signup_user_notification_subject', _x( '[%1$s] Activate %2$s', 'New user notification email subject' ), $user_login, $user_email, $key, $meta );
    wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );
    return true;
}

注意事项

  • $meta 参数存储为序列化数组,使用前需反序列化以访问数据。
  • 邮件内容和主题可通过过滤器自定义,确保内容格式适用于 wp_mail() 传输。
  • 函数依赖于多站点环境,仅在 WordPress 多站点安装中有效。

📄 原文内容

Sends a confirmation request email to a user when they sign up for a new user account (without signing up for a site at the same time). The user account will not become active until the confirmation link is clicked.

Description

This is the notification function used when no new site has been requested.

Filter ‘wpmu_signup_user_notification’ to bypass this function or replace it with your own notification behavior.

Filter ‘wpmu_signup_user_notification_email’ and ‘wpmu_signup_user_notification_subject’ to change the content and subject line of the email sent to newly registered users.

Parameters

$user_loginstringrequired
The user’s login name.
$user_emailstringrequired
The user’s email address.
$keystringrequired
The activation key created in wpmu_signup_user()
$metaarrayoptional
Signup meta data.

Default:array()

Return

bool

Source

function wpmu_signup_user_notification(
	$user_login,
	$user_email,
	#[SensitiveParameter]
	$key,
	$meta = array()
) {
	/**
	 * Filters whether to bypass the email notification for new user sign-up.
	 *
	 * @since MU (3.0.0)
	 *
	 * @param string $user_login User login name.
	 * @param string $user_email User email address.
	 * @param string $key        Activation key created in wpmu_signup_user().
	 * @param array  $meta       Signup meta data. Default empty array.
	 */
	if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) {
		return false;
	}

	$user            = get_user_by( 'login', $user_login );
	$switched_locale = $user && switch_to_user_locale( $user->ID );

	// Send email with activation link.
	$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         = sprintf(
		/**
		 * Filters the content of the notification email for new user sign-up.
		 *
		 * Content should be formatted for transmission via wp_mail().
		 *
		 * @since MU (3.0.0)
		 *
		 * @param string $content    Content of the notification email.
		 * @param string $user_login User login name.
		 * @param string $user_email User email address.
		 * @param string $key        Activation key created in wpmu_signup_user().
		 * @param array  $meta       Signup meta data. Default empty array.
		 */
		apply_filters(
			'wpmu_signup_user_notification_email',
			/* translators: New user notification email. %s: Activation URL. */
			__( "To activate your user, please click the following link:nn%snnAfter you activate, you will receive *another email* with your login." ),
			$user_login,
			$user_email,
			$key,
			$meta
		),
		site_url( "wp-activate.php?key=$key" )
	);

	$subject = sprintf(
		/**
		 * Filters the subject of the notification email of new user signup.
		 *
		 * @since MU (3.0.0)
		 *
		 * @param string $subject    Subject of the notification email.
		 * @param string $user_login User login name.
		 * @param string $user_email User email address.
		 * @param string $key        Activation key created in wpmu_signup_user().
		 * @param array  $meta       Signup meta data. Default empty array.
		 */
		apply_filters(
			'wpmu_signup_user_notification_subject',
			/* translators: New user notification email subject. 1: Network title, 2: New user login. */
			_x( '[%1$s] Activate %2$s', 'New user notification email subject' ),
			$user_login,
			$user_email,
			$key,
			$meta
		),
		$from_name,
		$user_login
	);

	wp_mail( $user_email, wp_specialchars_decode( $subject ), $message, $message_headers );

	if ( $switched_locale ) {
		restore_previous_locale();
	}

	return true;
}

Hooks

apply_filters( ‘wpmu_signup_user_notification’, string $user_login, string $user_email, string $key, array $meta )

Filters whether to bypass the email notification for new user sign-up.

apply_filters( ‘wpmu_signup_user_notification_email’, string $content, string $user_login, string $user_email, string $key, array $meta )

Filters the content of the notification email for new user sign-up.

apply_filters( ‘wpmu_signup_user_notification_subject’, string $subject, string $user_login, string $user_email, string $key, array $meta )

Filters the subject of the notification email of new user signup.

Changelog

Version Description
MU (3.0.0) Introduced.

User Contributed Notes