WordPress can send comment notification through email when it receives comments, but email notification may not be able to be viewed in time, so add a wechat notification.

Implementation method:

1. The first is notification via group chat robots on enterprise wechat.
2. The second is notification via server sauce

Both of these methods have their advantages and disadvantages, and there are many ways to notify through server sauce. But after all, you are using someone else’s service, which involves security, stability and the need for membership. Using your own enterprise wechat for notifications doesn’t need to worry about service unavailability, and there is no limit to the frequency of notifications. The downside is there’s only one option.

Code:

Enterprise wechat version:

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; Function commit_send($comment_id) {$comment = get_comment($comment_id); $url = 'webHook address '; $cont = $comment->comment_content; $title = $comment->comment_author; / / text message $data = array (" msgtype "= >" text ", "text" = > array (" content "= > $cont, "Mentioned_list "=>array("@all"); $data = array("msgtype"=>"news"); $data = array("msgtype"=>"news"); "News" = > array (" articles "= > array (array (" title =" "> date (" Y -m - d H: I: s", time ()). The "@". $title. "give you sent a comment", "description"=>$cont, "url"=>"https://cuixinxin.cn/about", / / jump address "picurl" = > "http://blogimg.lieme.cn/FmgJkCHkCJWHQdLXAXWjGL204IDx", / / graphic cover)))); $res = request_post($url, json_encode($data,'320'),'json'); } add_action('comment_post', 'commit_send', 19, 2);Copy the code

Server Sauce version:

Function commit_send($comment_id) {$comment = get_comment($comment_id); $text = '@'.$comment->comment_author.'; $desp = $comment->comment_content; $key = $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);Copy the code

Implementation effect

Enterprise wechat registration

I am a strange uncle selling nuts — a Buddha department front-end development, will be a lost lost photography, like toss, love food. Share tips, notes and a variety of interesting apps and resources tutorials ♥♥