函数文档

admin_created_user_email()

💡 云策文档标注

概述

admin_created_user_email() 函数用于生成 WordPress 管理员邀请用户加入站点时发送的电子邮件内容。它基于站点信息、用户角色和国际化支持,返回格式化的字符串,但不负责实际发送邮件。

关键要点

  • 函数接收一个参数 $text,但文档中未明确其用途,可能为占位符或保留参数。
  • 通过 get_editable_roles() 获取可编辑角色列表,并从 $_REQUEST['role'] 提取指定角色信息。
  • 使用 get_bloginfo('name') 获取站点标题,若无则从 home_url() 解析主机名作为备用。
  • 返回使用 sprintf() 和 __() 构建的国际化电子邮件模板,包含站点标题、URL 和用户角色的占位符。
  • 函数仅生成邮件内容,实际发送需依赖 wp_mail() 或其他邮件机制。

代码示例

function admin_created_user_email( $text ) {
    $roles = get_editable_roles();
    $role  = $roles[ $_REQUEST['role'] ];

    if ( '' !== get_bloginfo( 'name' ) ) {
        $site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
    } else {
        $site_title = parse_url( home_url(), PHP_URL_HOST );
    }

    return sprintf(
        /* translators: 1: Site title, 2: Site URL, 3: User role. */
        __(
            'Hi,
You've been invited to join '%1$s' at
%2$s with the role of %3$s.
If you do not want to join this site please ignore
this email. This invitation will expire in a few days.

Please click the following link to activate your user account:
%%s'
        ),
        $site_title,
        home_url(),
        wp_specialchars_decode( translate_user_role( $role['name'] ) )
    );
}

注意事项

  • 函数从 WordPress MU 3.0.0 版本引入,适用于多站点环境。
  • 依赖 $_REQUEST['role'] 获取角色,需确保请求中提供有效的角色键,否则可能导致错误。
  • 邮件模板包含占位符 %%s,实际使用时需替换为激活链接或其他动态内容。
  • 相关函数包括 get_editable_roles()、translate_user_role()、wp_specialchars_decode() 等,用于辅助处理角色和文本。

📄 原文内容

Parameters

$textstringrequired

Return

string

Source

function admin_created_user_email( $text ) {
	$roles = get_editable_roles();
	$role  = $roles[ $_REQUEST['role'] ];

	if ( '' !== get_bloginfo( 'name' ) ) {
		$site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
	} else {
		$site_title = parse_url( home_url(), PHP_URL_HOST );
	}

	return sprintf(
		/* translators: 1: Site title, 2: Site URL, 3: User role. */
		__(
			'Hi,
You've been invited to join '%1$s' at
%2$s with the role of %3$s.
If you do not want to join this site please ignore
this email. This invitation will expire in a few days.

Please click the following link to activate your user account:
%%s'
		),
		$site_title,
		home_url(),
		wp_specialchars_decode( translate_user_role( $role['name'] ) )
	);
}

Changelog

Version Description
MU (3.0.0) Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The admin_created_user_email function appears to be designed to generate the content for an email that is sent to a user when they are invited to join a WordPress site with a specific user role. Here’s what this function does in more detail:

    It retrieves a list of editable user roles on the WordPress site using the get_editable_roles() function and stores them in the $roles variable.

    It extracts the specific user role information based on a role key provided in the $_REQUEST array. This allows you to specify the user role when sending the invitation. The role information is stored in the $role variable.

    It determines the site title ($site_title) that will be included in the email. If the site has a name, it uses that name (decoded to handle special characters). If the site does not have a name, it uses the host part of the site’s URL.

    It constructs an email message template using sprintf() and the __() function for internationalization and translation purposes. The message includes placeholders for the site title, site URL, and user role.

    %1$s is a placeholder for the site title ($site_title).
    %2$s is a placeholder for the site’s URL (retrieved using home_url() ).
    %3$s is a placeholder for the user’s role name (after decoding special characters using wp_specialchars_decode() ) and translating the role name using translate_user_role() .
    The function returns the formatted email message as a string.

    It’s important to note that this function is responsible for generating the email content only; it doesn’t send the email itself. The actual sending of the email is typically handled elsewhere in your WordPress application, using functions like wp_mail() or a similar email sending mechanism. This function is intended to be used as part of a larger process for inviting users to your WordPress site with specific roles.