4

WordPressr如何实现评论消息推送至企业微信

 2 years ago
source link: https://www.huhexian.com/11525.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WordPressr如何实现评论消息推送至企业微信

青山 2021-11-1614:53:30评论1802字

我们在使用WordPress程序的时候,很多朋友都是开启评论功能的,我们看评论的话,每次都需要进入后台才可以查看,那么我们有什么简单的方法可以实现在手机上可以看评论吗?下面给大家带来 WordPressr如何实现评论消息推送至企业微信的方法教程,有需要的来看看吧!

WordPressr如何实现评论消息推送至企业微信WordPressr实现评论消息推送至企业微信方法:

在模板的function.php文件中增加下面代码(修改前记得备份):

  1. //评论微信推送-小灰灰
  2. function hui_send($comment_id){
  3. $comment = get_comment($comment_id);
  4. //获取评论内容
  5. $desp = $comment->comment_content;
  6. //获取评论时间
  7. $dataTime = $comment->comment_date;
  8. //获取评论人昵称
  9. $author = $comment->comment_author;
  10. //获取网站标题
  11. $web_name = get_bloginfo('name');
  12. //获取当前文章标题
  13. $title = get_the_title($comment->comment_post_ID);
  14. //获取当前文章的url地址
  15. $url = "https://xiaohuihui.net.cn/archives/".$comment->comment_post_ID.".html";
  16. //你的推送url地址【这里是企业微信群机器人api地址】
  17. $post_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=**********";
  18. $post_data = '{"msgtype": "markdown","markdown": {"content": "## <font color=\"red\" >【'.$web_name.'】有新评论啦<\/font>\n ><font color=\"warning\">标题:</font>'.$title.'\n ><font color=\"warning\">时间:</font>'.$dataTime.'\n ><font color=\"warning\">昵称:</font>'.$author.'\n ><font color=\"warning\">网址:</font>['.$url.']('.$url.')\n ><font color=\"warning\">评论内容:</font>\n <font color=\"info\" >'.$desp.'</font> "}}';
  19. return $res = curl_post($post_url, $post_data);
  20. add_action('comment_post', 'hui_send', 19, 2);
  21. //使用curl-post发送消息
  22. if (!function_exists('curl_post')){
  23. function curl_post($post_url, $post_data)
  24. $ch = curl_init();
  25. $header[] = "";
  26. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  27. curl_setopt($ch, CURLOPT_URL, $post_url);
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  29. curl_setopt($ch, CURLOPT_POST, 1);
  30. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  31. curl_setopt($ch, CURLOPT_USERAGENT,
  32. 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  35. $output = curl_exec($ch);
  36. curl_close($ch);
  37. return $output;

将上面上述代码放入文件最后,调试自己的API,然后进入自己的企业微信看看吧。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK