有些开放用户注冊功能的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. 路飞学城Python-Day15

    模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 提供还 ...

  2. Java正则类

    ava.util.regex 类 Pattern java.lang.Object 继承者 java.util.regex.Pattern 所有已实现的接口: Serializable public ...

  3. python 之 MRO 异常

    今天突然遇到这个异常,先贴两个地址,待有时间写博客 https://www.jianshu.com/p/fea6e0a0cc14 https://makina-corpus.com/blog/meti ...

  4. [APIO2016]Gap

    题目:UOJ#206. 题目大意:由于过于冗长,不好解释,所以详见原题. 解题思路:这是一道交互题. 对于第一问,很容易解决.由于数列严格递增,所以不会出现相等的情况. 首先调用MinMax(0,10 ...

  5. Ubuntu 如何进入系统文件/etc/profile修改内容

    转载:https://blog.csdn.net/cfq1491/article/details/81088117 /etc/profile 默认权限为 -rw-r--r-- 即只有root用户可以修 ...

  6. 双系统 windows引导项添加

    [root@MiWiFi-R2D-srv ~]# vi /etc/grub.d/40_custom #!/bin/sh exec tail -n +3 $0# This file provides a ...

  7. P1064 金明的预算方案 (依赖性背包问题)

    这道题可以用分组背包来做. 但是分组有两种方式 一种是把主件,主件+附件1,主件+附件2分成一组 组内只能选一个物品 一种是建一颗树,用树形dp的方式去做 第二种更通用,就算物品的依赖关系是森林都可以 ...

  8. ASP.NET-缓存基本知识点

    asp.net cache是一种缓存技术,然而,我们在asp.net程序中还可以使用其他的缓存技术,这些不同的缓存也各有所长.由于asp.net cache不能提供对外访问能力,因此,它不可能取代以m ...

  9. Ruby创建命令

    Ruby创建命令

  10. [MST] Describe Your Application Domain Using mobx-state-tree(MST) Models

    In this lesson, we introduce the running example of this course, a wishlist app. We will take a look ...