超簡單!兩步實(shí)現(xiàn)Wordpress評論微信通知~
戳上方“執(zhí)行上下文”,選擇“置頂公眾號”
關(guān)鍵時刻,第一時間送達(dá)!


WordPress收到評論的時候可以通過郵件發(fā)送評論通知,但是郵件通知的可能不是那么能及時的查看,所以增加一個微信通知。
實(shí)現(xiàn)方式:
1、第一種就是通過企業(yè)微信群聊機(jī)器人來通知。
2、第二種就是通過server醬來實(shí)現(xiàn)通知
這兩種方式各有好壞,通過server醬通知的方式很多,選擇性很多。但是畢竟是用的別人的服務(wù),涉及安全,穩(wěn)定以及需要會員的問題。用自己的企業(yè)微信來通知就不需要考慮服務(wù)不可用的問題,通知頻率也不會限制。缺點(diǎn)是選擇性就一個。
代碼:
企業(yè)微信版本:
date_default_timezone_set("Asia/Shanghai");
// 請求體
function request_post($url = '', $post_data = 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 = curl_exec($ch);
return $data;
}
// 調(diào)用代碼
function commit_send($comment_id) {
$comment = get_comment($comment_id);
$url = '企業(yè)微信機(jī)器人webHook地址';
// 評論內(nèi)容
$cont = $comment->comment_content;
// 評論人昵稱
$title = $comment->comment_author;
// 文本消息
$data = array(
"msgtype"=>"text",
"text"=>array(
"content"=>$cont,
"mentioned_list"=>array("@all") // 可以@群全部成員
)
);
// 圖文消息(二選一)
$data = array(
"msgtype"=>"news",
"news"=>array(
"articles"=>array(
array(
"title"=>date("Y-m-d H:i:s",time())."@".$title."給你發(fā)來一條評論",
"description"=>$cont,
"url"=>"https://cuixinxin.cn/about", // 跳轉(zhuǎn)地址
"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.'發(fā)來了一條評論';
$desp = $comment->comment_content;
$key = server醬的key;
$postdata = 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);
實(shí)現(xiàn)效果

企業(yè)微信注冊方式


我是賣堅(jiān)果的怪叔叔—— 一枚佛系前端開發(fā),會一丟丟攝影,喜歡折騰,愛好美食。分享點(diǎn)前端技巧、筆記以及各種有趣的APP和資源教程??
前端公眾號和交流群

評論
圖片
表情
