有些开放用户注册功能的WordPress站点,可能有这么一项需求,就是用户注册成功后,系统会分别给网站管理员和新用户发送一封通知邮件,给管理员发送的是新用户的用户名和Email,给刚刚注册的新用户发送的是他的用户名和密码。系统发送的邮件是纯文本类型的,页面不太美观,有没有办法发送自定义的HTML格式的邮件呢?答案是可以的。
WordPress给我们提供了一个可供插件重新定义的新用户邮件通知函数 wp_new_user_notification(),如果你不喜欢这个函数发送的邮件,我们可以重新定义这个函数的内容,以达到我们自定义的需求。
原函数
WordPress定义的这个函数内容是这样子的:
if ( !function_exists('wp_new_user_notification') ) :
/**
* Notify the blog admin of a new user, normally via email.
*
* @since 2.0
*
* @param int $user_id User ID
* @param string $plaintext_pass Optional. The user's plaintext password
*/
function wp_new_user_notification($user_id, $plaintext_pass = '') {
$user = get_userdata( $user_id );
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
// The blogname option is escaped with esc_html on the way into the database in sanitize_option
// we want to reverse this for the plain text arena of emails.
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$message = sprintf(__('New user registration on your site %s:'), $blogname) . "";
$message .= sprintf(__('Username: %s'), $user_login) . "";
$message .= sprintf(__('E-mail: %s'), $user_email) . "";
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message);
if ( empty($plaintext_pass) )
return;
$message = sprintf(__('Username: %s'), $user_login) . "";
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "";
$message .= wp_login_url() . "";
wp_mail($user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
}
endif;
我们可以在原函数的基础上,重新定义发送邮件的内容和格式,我们在当前主题的functions.php中加入重新定义的wp_new_user_notification函数即可,下面是一个示例,可以根据自己的需求进行修改: