wordpress修改新用户注册邮件内容的例子

发布时间:2020-04-24编辑:脚本学堂
本文介绍了wordpress中修改新用户注册邮件内容的方法,wordpress提供了一个可供插件重新定义的新用户邮件通知函数 wp_new_user_notification(),如果不喜欢这个函数发送的邮件,可以重新定义这个函数的内容。

wordpress修改新用户注册邮件内容的方法

一,自定义邮件内容和格式
有些开放用户注册功能的wordpress站点,用户注册成功后,系统会分别给网站管理员和新用户发送一封通知邮件,给管理员发送的是新用户的用户名和email,给刚刚注册的新用户发送的是他的用户名和密码。

系统发送的邮件是纯文本类型的,页面不太美观,有没有办法发送自定义的html格式的邮件呢?答案是可以的。

wordpress提供了一个可供插件重新定义的新用户邮件通知函数 wp_new_user_notification(),如果不喜欢这个函数发送的邮件,可以重新定义这个函数的内容。

2,原函数
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) . "rnrn";
    $message .= sprintf(__('username: %s'), $user_login) . "rnrn";
    $message .= sprintf(__('e-mail: %s'), $user_email) . "rn";

    @wp_mail(get_option('admin_email'), sprintf(__('[%s] new user registration'), $blogname), $message);

    if ( empty($plaintext_pass) )
        return;

    $message  = sprintf(__('username: %s'), $user_login) . "rn";
    $message .= sprintf(__('password: %s'), $plaintext_pass) . "rn";
    $message .= wp_login_url() . "rn";

    wp_mail($user_email, sprintf(__('[%s] your username and password'), $blogname), $message);

}
endif;

二,自定义邮件内容和格式
可以新建一个"插件",重新定义的wp_new_user_notification函数定义的邮件内容即可。
在wp-content/plugins/目录下,新建一个文本文件命名为new-user-notification.php,插入以下代码,保存,然后在后台启动插件new-user-notification即可:
 

复制代码 代码示例:

<?php
/*
  plugin name: new-user-notification
  description:重新定义发送邮件的内容和格式
  version: 1.0
 */

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);

    // 获取博客名称
    $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"n";
    @wp_mail(get_option('admin_email'), sprintf(__('[%s] new user registration'), $blogname), $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, sprintf(__('[%s] your username and password'), $blogname), $message, $message_headers);
}
endif;

?>
 

以上代码只是一个示例,可以根据需求进行修改。
至于html邮件该怎么写,什么样的邮件格式漂亮,大家自行研究下。