超简单!两步实现Wordpress评论微信通知~
WordPress收到评论的时候可以通过邮件发送评论通知,但是邮件通知的可能不是那么能及时的查看,所以增加一个微信通知。实现方式:
1、第一种就是通过企业微信群聊机器人来通知。 2、第二种就是通过server酱来实现通知 这两种方式各有好坏,通过server酱通知的方式很多,选择性很多。但是毕竟是用的别人的服务,涉及安全,稳定以及需要会员的问题。用自己的企业微信来通知就不需要考虑服务不可用的问题,通知频率也不会限制。缺点是选择性就一个。
代码:
企业微信版本:
date_default_timezone_set("Asia/Shanghai");
// 请求体
function request_post($url = '', $post_data = https://www.it610.com/article/array(),$dataType='') {
if (empty($url) || empty($post_data)) {
return false;
}
$curlPost = $post_data;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($dataType=='json'){
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded;
charset=UTF-8',
'Content-Length: ' . strlen($curlPost)
)
);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = https://www.it610.com/article/curl_exec($ch);
return $data;
}// 调用代码
function commit_send($comment_id){
$comment = get_comment($comment_id);
$url ='企业微信机器人webHook地址';
// 评论内容
$cont = $comment->comment_content;
// 评论人昵称
$title = $comment->comment_author;
// 文本消息
$data = https://www.it610.com/article/array("msgtype"=>"text",
"text"=>array(
"content"=>$cont,
"mentioned_list"=>array("@all") // 可以@群全部成员
)
);
// 图文消息(二选一)
$data = https://www.it610.com/article/array("msgtype"=>"news",
"news"=>array(
"articles"=>array(
array(
"title"=>date("Y-m-d H:i:s",time())."@".$title."给你发来一条评论",
"description"=>$cont,
"url"=>"https://cuixinxin.cn/about", // 跳转地址
"picurl"=>"http://blogimg.lieme.cn/FmgJkCHkCJWHQdLXAXWjGL204IDx", // 图文消息封面
)
)
)
);
$res = request_post($url, json_encode($data,'320'),'json');
}
add_action('comment_post', 'commit_send', 19, 2);
server酱版本:
function commit_send($comment_id){
//server酱
$comment = get_comment($comment_id);
$text = '@'.$comment->comment_author.'发来了一条评论';
$desp = $comment->comment_content;
$key = server酱的key;
$postdata = https://www.it610.com/article/http_build_query(
array('text' => $text,
'desp' => $desp
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
return $result = file_get_contents('https://sctapi.ftqq.com/'.$key.'.send', false, $context);
}
add_action('comment_post', 'commit_send', 19, 2);
实现效果
文章图片
企业微信注册方式
文章图片
文章图片
【超简单!两步实现Wordpress评论微信通知~】我是卖坚果的怪叔叔—— 一枚佛系前端开发,会一丢丢摄影,喜欢折腾,爱好美食。分享点前端技巧、笔记以及各种有趣的APP和资源教程??
推荐阅读
- 为什么你的路演总会超时()
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 科学养胃,别被忽悠,其实真的很简单
- opencv|opencv C++模板匹配的简单实现
- 松软可口易消化,无需烤箱超简单,新手麻麻也能轻松成功~
- 原生家庭之痛与超越
- 简单心理2019春A期+32+张荣
- 《算法》-图[有向图]
- BNC公链|BNC公链 | Eth2.0测试网Topaz已质押超100万枚ETH
- android防止连续点击的简单实现(kotlin)