WordPress修改新用户注册邮件内容教程

WordPress修改新用户注册邮件内容教程,第1张

概述有些开放用户注册功能的WordPress站点,可能有这么一项需求,就是用户注册成功后,系统会分别给网站管理员和新用户发送一封通知邮件,给管理员发送的是新用户的用户名和Email,给刚刚注册的新用户发送的是他的用户名和密码。系统发送的邮件是纯文本类型的,页面不太美观,有没有办法发送自定义的HTML…

有些开放用户注册功能的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'),$message);

}
endif;自定义邮件内容和格式

我们可以在原函数的基础上,重新定义发送邮件的内容和格式,我们在当前主题的functions.PHP中加入重新定义的wp_new_user_notification函数即可,下面是一个示例,可以根据自己的需求进行修改

if ( !function_exists('wp_new_user_notification') ) :
/**
* Notify the blog admin of a new user,$plaintext_pass = '') {
$user = get_userdata( $user_ID );

$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);

// 获取博客名称
$blogname = wp_specialchars_decode(get_option('blogname'),ENT_QUOTES);

// 给管理员发送的邮件内容,这里是HTML格式
$message = '<HTML><head><Meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><Title>新用户注册</Title></head><body><div align="center"><table cellpadding="0" cellspacing="1" style="border:3px solID #d9e9f1;background:#7fbddd; text-align:left;"><tr><td style="padding:0;"><table cellpadding="30" cellspacing="0" style="border:1px solID #ffffff;background:#f7f7f7;wIDth:500px;"><tr><td style="line-height:2;Font-size:12px;">您的网站 <strong>' . $blogname . '</strong> 有新用户注册。<br />用户名:' . $user_login . '<br />Email:' . $user_email . '<br /><br />如果您不是 <strong>' . $blogname . '</strong> 的管理员,请直接忽略本邮件!</div></td></tr></table></td></tr></table></div></body></HTML>';

// 给网站管理员发送邮件
$message_headers = "Content-Type: text/HTML; charset="utf-8"";
@wp_mail(get_option('admin_email'),$message,$message_headers);

if ( empty($plaintext_pass) )
return;

// 你可以在此修改发送给新用户的通知Email,这里是HTML格式
$message = '<HTML><head><Meta http-equiv="Content-Type" content="text/HTML; charset=utf-8"><Title>新用户注册</Title></head><body><div align="center"><table cellpadding="0" cellspacing="1" style="border:3px solID #d9e9f1;background:#7fbddd; text-align:left;"><tr><td style="padding:0;"><table cellpadding="30" cellspacing="0" style="border:1px solID #ffffff;background:#f7f7f7;wIDth:500px;"><tr><td style="line-height:2;Font-size:12px;">您刚刚在网站 <strong>' . $blogname . '</strong> 注册一个帐号。<br />用户名:' . $user_login . '<br />登录密码:' . $plaintext_pass . '<br />登录网址:<a href="' . wp_login_url() . '">' . wp_login_url() . '</a><br /><br />如果您没有在 <strong>'. $blogname . '</strong> 注册过任何信息,请直接忽略本邮件!</div></td></tr></table></td></tr></table></div></body></HTML>';

// sprintf(__('[%s] Your username and password'),$blogname) 为邮件标题
wp_mail($user_email,$message_headers);
}
endif;

至于HTML邮件该怎么写,什么样的邮件格式漂亮,这些就自己琢磨吧。

总结

以上是内存溢出为你收集整理的WordPress修改新用户注册邮件内容教程全部内容,希望文章能够帮你解决WordPress修改新用户注册邮件内容教程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/1003215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存