WordPress 邮件通知站内所有用户和访客代码
首页>>技术教程>>正文
本篇文章更新于 2022-04-21 18:07:45
本篇文章更新于 2022-04-21

在主题的function.php文件中添加下面的代码

//邮件通知
/**
 * 
 *@foemmail 1
 *
 */
add_action( 'submitpost_box', 'fo_submit_box');
function fo_submit_box(  ){
	echo '<div id="fo_side-sortables" class="meta-box-sortables ui-sortable">';
	echo '<div id="fo_submit_box" class="postbox ">';
	echo '<div class="handlediv" title="点击以切换"><br /></div>';
	echo '<h3 class="hndle"><span>邮件通知</span></h3>';
	echo '<div class="inside"><div class="submitbox">';
	echo '  <div style="padding: 10px 10px 0;text-align: left;"><label class="selectit" title="慎用此功能,重要文章才勾选嘛,以免引起读者反感哈"><input type="checkbox" unchecked name="FO_emaill_report_user" value="true" title="勾选此项,将邮件通知博客所有评论者"/>邮件通知博客所有评论者</label></div>';
	echo '</div></div>';
	echo '</div>';
	echo '</div>';
}
/**
 * 
 *@foemmail 2
 *
 */
add_action( 'publish_post', 'fo_emaill_report_users' );
function fo_emaill_report_users($post_ID)
{
	//如果未勾选保存远程图片,不进行任何操作
	if($_POST['FO_emaill_report_user'] != 'true'){
		return;
	}

	//修订版本不通知,以免滥用
	if( wp_is_post_revision($post_ID) ){
		return;
	}

	//获取wp数据操作类
	global $wpdb;
	// 读数据库,获取所有用户的email
	$wp_user_emails = $wpdb->get_results("SELECT DISTINCT comment_author, comment_author_email FROM $wpdb->comments WHERE TRIM(comment_author_email) IS NOT NULL AND TRIM(comment_author_email) != ''");
	
	// 获取博客名称
	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
	// 获取博客URL
	$blogurl = get_bloginfo("siteurl");

	//文章链接
	$post_link = get_permalink($post_ID);
	//文章标题$post -> post_title
	$post_title = strip_tags($_POST['post_title']);
	//文章内容$post->post_content
	$post_content = strip_tags($_POST['post_content']);
	//文章摘要
	$output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,200}).*/s','\1',$post_content).'......';

	//邮件头,以免乱码
	$message_headers = "Content-Type: text/html; charset=\"utf-8\"\n";
	// 邮件标题
	$subject = '《'.$blogname.'》有新文章发表了,速来围观。';
	
	foreach ( $wp_user_emails as $wp_user_email )
	{
		// 邮件内容
		$message = '
			<div style="MARGIN-RIGHT: auto; MARGIN-LEFT: auto;">
			<strong style="line-height: 1.5; font-family:Microsoft YaHei;">
			亲爱的'.$wp_user_email->comment_author.':
			</strong>
			<p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
				您曾经来访过的博客《'.$blogname.'》有新文章发表了。
			</p>
			<p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
				文章标题:<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
				<br />
				文章摘要:'.$output.'
			</p>
			<p style="FONT-SIZE: 14px; PADDING-TOP: 6px">
				您可以点击链接
				<a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
				>
				<a title="'.$post_title.'" href="'.$post_link.'" target="_top">'.$post_title.'</a>
				详细查看
			</p>
			<p style="font-size: 14px; padding-top: 6px; text-align: left;">
				<span style="line-height: 1.5; color: rgb(153, 153, 153);">
				来自:
				</span>
				<a href="'.$blogurl.'" style="line-height: 1.5;">'.$blogname.'</a>
			</p>
			<div style="font-size: 12px; border-top-color: rgb(204, 204, 204); border-top-width: 1px; border-top-style: solid; height: 35px; width: 500px; color: rgb(102, 102, 102); line-height: 35px; background-color: rgb(245, 245, 245);">
				该邮件为系统发送邮件,请勿直接回复!如有打扰,请向博主留言反映。灰常感谢您的阅读!
			</div>
			</div>';
		
		wp_mail($wp_user_email->comment_author_email, $subject, $message, $message_headers);
	}
}

然后在发表文章的时候选中邮件通知即可--请在经典编辑器中使用

历史上的今天

很遗憾~,历史上的今天未发表过文章
技术教程

文章增加角标

2022-4-16 18:22:08

技术教程

wordpress使用cdn后获取访客真实ip

2022-4-21 18:09:16

重要说明

本站资源大多来自网络,如有侵犯你的权益请联系管理员或给邮箱发送邮件zhouqiuqiu123@foxmail.com 我们会第一时间进行审核删除。站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果遇到评论下载的文章,评论后刷新页面点击对应的蓝字按钮即可跳转到下载页面本站资源少部分采用7z压缩,为防止有人压缩软件不支持7z格式,7z解压,建议下载7-zip(点击下载),zip、rar解压,建议下载WinRAR(点击下载)

敏而好学,不耻下问。——孔子