基于“Server酱”实现WordPress新评论微信提醒

这次重装系统后不管怎么配置,SMTP服务就是不工作,这就意味着我不能用邮箱推送站点的注册消息,评论消息等。就有了以下的解决办法。
“Server酱” 混恩山的盆友应该不会陌生,英文名「ServerChan」, 是一款「程序员」和「服务器」之间的通信软件。通俗点讲,就是从服务器推送报警和日志到手机的工具。hiboy大佬的老毛子固件就集成了这一功能,我一直用它来推送新固件提醒和新设备的接入提醒。

如果是第一次使用此服务要做的是获得一个 SCKEY,步骤:

  • 登入:用 GitHub 账号登入网站,就能获得一个 SCKEY(在「发送消息」页面);
  • 绑定:点击「微信推送」,扫码关注同时即可完成绑定;
/**
*基于“Server酱”的新评论微信推送
*2018-04-06 by http://guihet.com
*/
function sc_send($comment_id)
{
        $text = 'Guihet-上有新的评论';
        $comment = get_comment($comment_id);
        if(($comment->comment_author_email) == '站长你的邮箱'){
                return 'false';
        }
        $desp ='《'.get_the_title($comment->comment_post_ID) ."》  \n"
                .'评论者:'.$comment->comment_author."  \n"
                .'评论内容:'. $comment->comment_content;
        $key = '你的 SCKEY';
        $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('http://sc.ftqq.com/'.$key.'.send', false, $context);
}
add_action('comment_post', 'sc_send', 19, 2);

以上代码添加到 function.php文件保存即可。
此功能还可以提醒新用户注册等更多信息,有待进一步开发..
04-06:PHP语法–>分号结尾,无视换行,随便回车。。。。。
增加判断站长自己的评论不提醒,配合“防止冒充管理员评论”效果更好
03-24:改进了输出的文本格式
添加评论的文章,添加评论者,以及格式化输出结果
其中文本支持Markdown,两个空格+\n 就是结果中的换行显示。

防止冒充管理员评论

实现管理多个管理员的昵称多个管理员的邮箱

/**
*禁止冒充管理员评论
*2018-04-06 by http://guihet.com
*/
function guihet_usecheck($incoming_comment) {
    $isSpam = 0;
    $admin = array("鬼手六", "其他管理员");
    $adminEmail = array('站长的邮箱', '其他管理的邮箱@qq.com');
    if (in_array(trim($incoming_comment['comment_author']), $admin))
        $isSpam = 1;
    if(in_array(trim($incoming_comment['comment_author_email']), $adminEmail))
        $isSpam = 1;
    if(!$isSpam)
        return $incoming_comment;
    wp_die('如果你是管理员请登录后进行评论,否则请勿冒充本站管理发表评论 !!!');
}
if(!is_user_logged_in())
	add_filter( 'preprocess_comment', 'guihet_usecheck' );

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

  1. 测试 说道:

    :huaji3: 测试

    1. 信爷 说道:

      OwO表情不能用的,直接使用替换自带图片名称的方式做好了..