函数文档

newblog_notify_siteadmin()

💡 云策文档标注

概述

newblog_notify_siteadmin() 是一个 WordPress 函数,用于在网络中激活新站点时向网络管理员发送通知邮件。它检查配置选项、生成邮件内容,并通过 apply_filters 钩子允许自定义邮件正文。

关键要点

  • 函数功能:当新站点激活时,向网络管理员发送通知邮件。
  • 参数:接受 $blog_id(新站点的对象或 ID)和 $deprecated(已弃用,未使用)。
  • 返回值:布尔值,成功发送邮件返回 true,否则返回 false。
  • 钩子:使用 apply_filters('newblog_notify_siteadmin', $msg, $blog_id) 过滤邮件正文,允许开发者自定义内容。
  • 依赖函数:包括 get_site_option、wp_mail、switch_to_blog 等,用于检查设置、发送邮件和切换博客上下文。

代码示例

function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
    if ( is_object( $blog_id ) ) {
        $blog_id = $blog_id->blog_id;
    }

    if ( 'yes' !== get_site_option( 'registrationnotification' ) ) {
        return false;
    }

    $email = get_site_option( 'admin_email' );

    if ( ! is_email( $email ) ) {
        return false;
    }

    $options_site_url = esc_url( network_admin_url( 'settings.php' ) );

    switch_to_blog( $blog_id );
    $blogname = get_option( 'blogname' );
    $siteurl  = site_url();
    restore_current_blog();

    $msg = sprintf(
        __(
            'New Site: %1$s
URL: %2$s
Remote IP address: %3$s

Disable these notifications: %4$s'
        ),
        $blogname,
        $siteurl,
        wp_unslash( $_SERVER['REMOTE_ADDR'] ),
        $options_site_url
    );
    $msg = apply_filters( 'newblog_notify_siteadmin', $msg, $blog_id );

    wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );

    return true;
}

注意事项

  • 函数仅在网络设置中的 'registrationnotification' 选项为 'yes' 时执行,否则返回 false。
  • 邮件接收地址从 'admin_email' 选项获取,需验证为有效邮箱。
  • 使用 switch_to_blog 和 restore_current_blog 来临时切换博客上下文以获取新站点信息。
  • 邮件正文可通过 'newblog_notify_siteadmin' 过滤器自定义,支持传递 $blog_id 参数。

📄 原文内容

Notifies the network admin that a new site has been activated.

Description

Filter ‘newblog_notify_siteadmin’ to change the content of the notification email.

Parameters

$blog_idWP_Site|intrequired
The new site’s object or ID.
$deprecatedstringrequired
Not used.

Return

bool

Source

function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
	if ( is_object( $blog_id ) ) {
		$blog_id = $blog_id->blog_id;
	}

	if ( 'yes' !== get_site_option( 'registrationnotification' ) ) {
		return false;
	}

	$email = get_site_option( 'admin_email' );

	if ( ! is_email( $email ) ) {
		return false;
	}

	$options_site_url = esc_url( network_admin_url( 'settings.php' ) );

	switch_to_blog( $blog_id );
	$blogname = get_option( 'blogname' );
	$siteurl  = site_url();
	restore_current_blog();

	$msg = sprintf(
		/* translators: New site notification email. 1: Site URL, 2: User IP address, 3: URL to Network Settings screen. */
		__(
			'New Site: %1$s
URL: %2$s
Remote IP address: %3$s

Disable these notifications: %4$s'
		),
		$blogname,
		$siteurl,
		wp_unslash( $_SERVER['REMOTE_ADDR'] ),
		$options_site_url
	);
	/**
	 * Filters the message body of the new site activation email sent
	 * to the network administrator.
	 *
	 * @since MU (3.0.0)
	 * @since 5.4.0 The `$blog_id` parameter was added.
	 *
	 * @param string     $msg     Email body.
	 * @param int|string $blog_id The new site's ID as an integer or numeric string.
	 */
	$msg = apply_filters( 'newblog_notify_siteadmin', $msg, $blog_id );

	/* translators: New site notification email subject. %s: New site URL. */
	wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );

	return true;
}

Hooks

apply_filters( ‘newblog_notify_siteadmin’, string $msg, int|string $blog_id )

Filters the message body of the new site activation email sent to the network administrator.

Changelog

Version Description
MU (3.0.0) MU (3.0.0)
5.1.0 Introduced.