WordPress评论留言微信消息推送提醒

佚名 2020-12-27 2,113 12/27

博客有人发布留言之后就自动发送评论到站长的个人微信上面!而且有提示音,和普通发微信给你一样的效果!

具体代码

add_action('comment_post', 'comment_WeChat_notify');

function comment_WeChat_notify($comment_id){
    $comment = get_comment($comment_id);
    $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
    $spam_confirmed = $comment->comment_approved;
    function send_post($url, $post_data) {
        $postdata = http_build_query($post_data);
        $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间(单位:s)
        )
      );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
        $url = get_option('home');
        $description = "<div class='gray'>有人在《".get_the_title($comment->comment_post_ID)."》给您留言啦!<br>".trim($comment->comment_author)."说:".trim($comment->comment_content)."</div>";
        $post_data = array(
            'corpid'=>'****',
            'corpsecret'=>'***',
            'agentid'=>'***',
            'title'=>'有人在你的博客上评论留言啦!',
            'description'=>$description,
            'url'=>$url
        );
        send_post('https://api.htm.fun/api/Wechat/text_card/',$post_data);
}

使用教程

替换代码中$post_data中的***。然后丢在主题的 functions.php中即可!

登录企业微信网站,在我的企业里有企业ID,就是corpid,corpsecret、agentid为企业微信应用api,企业应用需要自己创建一个。

WordPress评论留言微信消息推送提醒

- THE END -

佚名

7月14日11:50

最后修改:2021年7月14日
0

共有 0 条评论