有些开放用户注冊功能的WordPress站点,可能有这么一项需求,就是用户注冊成功后,系统会分别给站点管理员和新用户发送一封通知邮件。给管理员发送的是新用户的username和Email,给刚刚注冊的新用户发送的是他的username和password。系统发送的邮件是纯文本类型的,页面不太美观,有没有办法发送自己定义的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) . "\r\n\r\n";
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n"; @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), $blogname), $message); if ( empty($plaintext_pass) )
return; $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
$message .= wp_login_url() . "\r\n"; 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 />登陆password:' . $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邮件该怎么写,什么样的邮件格式美丽。这些就自己琢磨吧。

WordPress改动新用户注冊邮件内容--自己定义插件的更多相关文章

  1. struts2学习笔记(三)—— 在用户注冊程序中使用验证框架

    实现目标:       1.使用验证框架对用户注冊信息进行验证       2.验证username.password.邮箱不能为空       3.验证username.password长度     ...

  2. Django 介绍、安装配置、基本使用、Django 用户注冊样例

    Django介绍         Django 是由 Python 开发的一个免费的开源站点框架.能够用于高速搭建高性能.优雅的站点.              DjangoMTV 的思想项目架构图 ...

  3. YII用户注冊和用户登录(三)之模型中规则制定和分析

    3 模型中规则制定和分析 YII模型主要分为两类,一个数据模型,处理和数据库相关的增删改查.继承CActiveRecord.还有一个是表单模型,继承CFormModel.不与数据库进行交互.操作与数据 ...

  4. 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)

    近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...

  5. YII用户注冊表单的实现熟悉前台各个表单元素操作方式

    模还是必须定义两个基本方法.还有部分label标签映射为汉字,假设进行表单验证,还要定义一些验证规则: <? php /* * 用户模型 * */ class user extends CAct ...

  6. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  7. YII用户注冊和用户登录(二)之登录和注冊在视图通过表单使用YII小物件并分析

    2 登录和注冊在视图通过表单使用YII小物件并分析 <?php $form = $this -> beginWidget('CActiveForm', array( 'enableClie ...

  8. Android实战简易教程-第二十三枪(基于Baas的用户注冊和登录模块实现!)

    接着上两篇文章.我们基于Bmob提供的API实现用户登录功能.总体看一下代码. 1.注冊页面xml: <RelativeLayout xmlns:android="http://sch ...

  9. YII用户注冊和用户登录(五)之进行session和cookie分析 ,并在前后区分session和cookie

    5 进行session和cookie分析 ,并在前后区分session和cookie: 记住登录状态 这样下次再登录站点的时候.就不用反复输入username和password. 是浏览器的cooki ...

随机推荐

  1. iOS开发者中心重置设备列表

    苹果开发者账号允许的测试设备为100台,如果你注册了,这台机器就算是一个名额,禁用也算一个名额,仍被计入机器总数,每年可以重置一次,那我们怎么重置机器数量呢? 我们需要给苹果发送申请: https:/ ...

  2. HDU-4221 Greedy? 贪心 从元素的相对位置开始考虑

    题目链接:https://cn.vjudge.net/problem/HDU-4221 题意 给n个活动,每个活动需要一段时间C来完成,并且有一个截止时间D 当完成时间t大于截止时间完成时,会扣除t- ...

  3. GenIcam标准(三)

    2.6. 缓存 如果某个实现对每个写操作支持范围.实现和可用状态的检查,通常会触发一系列对相机的读操作.大多数用于有效性检查的数值很少或不会发生变化,所以可以放入缓存.相机描述文件包含所有必需的定义以 ...

  4. HDU--4891--The Great Pan--暴力搜索

    The Great Pan Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. linux清除邮件队列

    [root@localhost mail]#tmp=`mailq | grep -E "root" | awk '{print $1}'` [root@localhost mail ...

  6. Ignatius and the Princess III(杭电1028)(母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. Android触碰事件

    OnTouchListener使用 public class ViewActivity extends Activity implements View.OnTouchListener { @Over ...

  8. Getting Started with MongoDB (C# Edition)

    https://docs.mongodb.com/getting-started/csharp/ 概览 Welcome to the Getting Started with MongoDB guid ...

  9. nyoj--218--Dinner(语法)

    Dinner 时间限制:100 ms  |  内存限制:65535 KB 难度:1 描述 Little A is one member of ACM team. He had just won the ...

  10. 5. MongoDB基本操作语句

    转自:http://blog.51cto.com/shanqiangwu/1653577 #MongoDB中有三元素:数据库,集合,文档,其中“集合”就是对应关系数据库中的“表”,“文档”对应“行”. ...