newuser_notify_siteadmin()
云策文档标注
概述
newuser_notify_siteadmin() 是一个 WordPress 函数,用于在网络管理员启用注册通知时,向网络管理员发送新用户激活的电子邮件通知。该函数通过过滤钩子 newuser_notify_siteadmin 允许开发者自定义通知内容。
关键要点
- 函数功能:当新用户激活时,向网络管理员发送通知邮件,前提是网络设置中的 registrationnotification 选项为 'yes'。
- 参数:接受一个必需参数 $user_id(新用户的 ID),返回布尔值表示通知是否成功发送。
- 过滤钩子:提供 newuser_notify_siteadmin 过滤钩子,允许修改邮件正文内容,参数包括 $msg(邮件正文)和 $user(WP_User 对象)。
- 依赖函数:使用 get_site_option、is_email、get_userdata、network_admin_url、esc_url、wp_unslash、__、wp_mail 等核心函数来构建和发送邮件。
代码示例
function newuser_notify_siteadmin( $user_id ) {
if ( 'yes' !== get_site_option( 'registrationnotification' ) ) {
return false;
}
$email = get_site_option( 'admin_email' );
if ( ! is_email( $email ) ) {
return false;
}
$user = get_userdata( $user_id );
$options_site_url = esc_url( network_admin_url( 'settings.php' ) );
$msg = sprintf(
__(
'New User: %1$s
Remote IP address: %2$s
Disable these notifications: %3$s'
),
$user->user_login,
wp_unslash( $_SERVER['REMOTE_ADDR'] ),
$options_site_url
);
$msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
wp_mail( $email, sprintf( __( 'New User Registration: %s' ), $user->user_login ), $msg );
return true;
}注意事项
- 函数仅在网络设置中 registrationnotification 选项为 'yes' 时执行,否则返回 false。
- 邮件发送依赖于 wp_mail 函数,需确保邮件配置正确。
- 使用 newuser_notify_siteadmin 过滤钩子时,可以修改邮件正文,但需注意保持格式和安全性。
- 函数从 WordPress MU 3.0.0 版本引入,适用于多站点网络环境。
原文内容
Notifies the network admin that a new user has been activated.
Description
Filter ‘newuser_notify_siteadmin’ to change the content of the notification email.
Parameters
$user_idintrequired-
The new user’s ID.
Source
function newuser_notify_siteadmin( $user_id ) {
if ( 'yes' !== get_site_option( 'registrationnotification' ) ) {
return false;
}
$email = get_site_option( 'admin_email' );
if ( ! is_email( $email ) ) {
return false;
}
$user = get_userdata( $user_id );
$options_site_url = esc_url( network_admin_url( 'settings.php' ) );
$msg = sprintf(
/* translators: New user notification email. 1: User login, 2: User IP address, 3: URL to Network Settings screen. */
__(
'New User: %1$s
Remote IP address: %2$s
Disable these notifications: %3$s'
),
$user->user_login,
wp_unslash( $_SERVER['REMOTE_ADDR'] ),
$options_site_url
);
/**
* Filters the message body of the new user activation email sent
* to the network administrator.
*
* @since MU (3.0.0)
*
* @param string $msg Email body.
* @param WP_User $user WP_User instance of the new user.
*/
$msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
/* translators: New user notification email subject. %s: User login. */
wp_mail( $email, sprintf( __( 'New User Registration: %s' ), $user->user_login ), $msg );
return true;
}
Hooks
- apply_filters( ‘newuser_notify_siteadmin’, string $msg, WP_User $user )
-
Filters the message body of the new user activation email sent to the network administrator.
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |