一般wordpress评论通知都是通过邮件通知的,但是没有实时推送的效果。以前有个飞信接口,但是太抠门了,没有提供官方的接口,个人做的接口总会过段时间失效。今天在网上看到网友利用Pushbullet推送Wordpress评论至手机上,很有意思。
Pushbullet介绍
官方页面:https://www.pushbullet.com
Pushbullet (子弹推送) 是一个强大免费的跨平台消息推送工具 (服务),目前支持 Android、iOS、Windows、Mac 等系统。它最大特色是能支持消息双向甚至是多向推送!你可以在不同设备间来回互相推送文字、图片、网址、地址、短信、电话号码、待办事项列表等,在转换设备使用时特别方便!在同类软件中表现出色,绝对是工作、生活上都能提高效率的利器……
Pushbullet 使用谷歌帐号进行登录,如果你已经有帐号则不需再注册了。
实现推送需要在手机上安装客户端,安卓及ios客户端均可在市场下载到,这里就不提供链接了。
使用Pushbullet推送wordpress评论
1、借助zapier
zapier号称商务版的IFTTT,它让非懂技术用户也能毫不费力地在原本不相关的API之间建立关联。
地址:Zapier 。
这方法虽然简单,但是有1-15分钟的延迟,而且还要提供Zapier WordPress帐号和密码。
2、使用插件。
Pushbullent有自己的Wordpress插件,但是功能比较多,不喜欢折腾的同学可以使用这种方法。
插件地址:https://wordpress.org/plugins/pushbullet-notification/
3、wordpress代码实现
首先获取Apikey及设备ID
登陆后,点击 这里 找到你的Apikey
在首页选中推送设备,可以再地址栏看到设备id
在functions.php添加如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
//pushbullet推送评论 function pushbullet($title,$body) { $apikey='你的apikey'; $device_iden='你的设备id'; $req_args = array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $apikey.':') ), 'timeout' => 50, 'sslverify' =>FALSE, 'method' => 'post', 'body'=>array( 'type' => 'note', 'device_iden'=>$device_iden, 'title' => $title, 'body'=>$body) ); $response = wp_remote_post( 'https://api.pushbullet.com/api/pushes', $req_args ); } function tennfy_push($comment_ID) { $comment = get_comment($comment_ID); if (empty($comment)) { return; } if ($comment->comment_approved != 'spam') { $post = get_post($comment->comment_post_ID); if ($comment->user_id == $post->post_author) { return; } if ('pingback' == $comment->comment_type || 'trackback' == $comment->comment_type) { return; } $title = $comment->comment_author . " 评论了 《" . get_the_title($comment->comment_post_ID) . "》"; $body = "文章:《" . get_the_title($comment->comment_post_ID) . "》\n评论: " . trim($comment->comment_content) . "\n链接: " . get_permalink($comment->comment_post_ID) . '#comment-' . $comment_ID; pushbullet($title,$body); } } add_action('comment_post', 'tennfy_push', 10, 2); |
总结
pushbullet提供了api可供我们把玩,手机上安装客户端之后,可以实现推送微博,评论等等功能。
参考文章:巧用Pushbullent推送Wordpress评论