利用方糖提供得到服务号接口给自己网站增加一个快捷,简单的微信订阅功能,让访客触手可及。
前言
很多人都知道之前有一个叫server酱的玩意,很多人给自己网站增加了这个功能,主要是给管理员提供评论推送功能的,这里面也包括云落,前段时间发现原来server酱还有升级版的东西,就是今天了要说的PushBbar,于是云落顺手将pushbear也加入到WordPress里面。
介绍
pushbear是一个基于微信模板的一对多消息送达服务,简单说就是微信群发功能,是使用的方糖气球来发送的,我们只需要向pushbear对应的接口发送一个get,就能将信息群发出去。到达率极高,基本是实时到达。
使用
pushbear的使用非常简单。
注意事项
- 推送消息存储72小时
- 5分钟内不可发布重复消息
- 短时间发送过多内容会被打包汇总推送
- 普通用户每天1000条上限
- 请勿用于发送广告和有害信息
- 默认二维码自己识别内容后重新制作美化
- 每天不能发送过多内容,不然服务器IP会被临时屏蔽
- 正文支持MD,大小不超过64kb
- 免费
- 介意自己用户关注别人公众号的慎用
代码版
云落将代码整理一下,Git主题用户不要使用,主题已有该功能,不要重复添加
//微信订阅推送 function pushbear_send($post_ID) { if (get_post_meta($post_ID, 'wx_pushbear_key', true) == 1) return; $text = get_the_title($post_ID); //微信推送信息标题 $wx_post_link = get_permalink($post_ID).'?from=pushbear';//文章链接 $wx_post_content = mb_strimwidth(strip_tags(strip_shortcodes(get_post($post_ID)->post_content)) , 0, 210 , '……'); $desp = '>'.$wx_post_content.' *** [【点击链接查看全文】]('.$wx_post_link.')'; //微信推送内容正文 $key = '546546546546456456465465464';//这里写入自己获取的Key $request = new WP_Http; $api_url = 'https://pushbear.ftqq.com/sub'; $body = array( 'sendkey' => $key, 'text' => $text, 'desp' => $desp ); $headers = 'Content-type: application/x-www-form-urlencoded'; $result = $request->post($api_url, array( 'body' => $body, 'headers' => $headers ) ); if(!is_wp_error($result)){ add_post_meta($post_ID, 'wx_pushbear_key', 1, true); } } add_action('publish_post', 'pushbear_send');
上面代码是默认自动推送的,但是实际上云落是不推荐的,因为肯定有的文章是不适合给所有的订阅者查看的,但是这个云落没有在代码里面多谢,这个加一个自定义栏目就可以的,为了方便一些不太会代码的朋友使用,云落写了个小插件方便使用,就在下面。
插件版
插件版只是方便使用,方便控制,其实区别不大的,主要看下几方面。
首先在设置最底部输入PushBear的key
其实是在发布文章的时候勾选好推行推送
//建立box function wxpb_register_meta_boxes(){ add_meta_box('pushbear-meta-box', '微信订阅推送', 'wxpb_my_display_callback', 'post', 'side', 'low'); } add_action('add_meta_boxes', 'wxpb_register_meta_boxes'); //回调函数 function wxpb_my_display_callback($post){ $value = get_post_meta($post->ID, 'wx_pushbear_key', true); if ($value) { $check = ' checked="checked"'; } else { $check = ''; } ?> <input name="wx_pushbear_field" type="checkbox"<?php echo $check;?> value="1" /> 启用微信订阅推送 <?php } //增加后台设置 $new_general_setting = new new_general_setting(); class new_general_setting { function new_general_setting( ) { add_filter( 'admin_init' , array( &$this , 'register_fields' ) ); } function register_fields() { register_setting( 'general', 'pushbear_key', 'esc_attr' ); add_settings_field('pushbear_key', '<label for="pushbear_key">PushBear Key</label>' , array(&$this, 'fields_html') , 'general' ); } function fields_html() { $value = get_option( 'pushbear_key', '' ); echo '<input type="text" id="pushbear_key" name="pushbear_key" value="' . $value . '" />'; } } //微信订阅推送 function pushbear_send($post_ID) { if (get_post_meta($post_ID, 'wx_pushbear_key', true) == 1) return; if($_POST['wx_pushbear_field'] != 1) return; $text = get_the_title($post_ID); //微信推送信息标题 $wx_post_link = get_permalink($post_ID).'?from=pushbear';//文章链接 $wx_post_content = mb_strimwidth(strip_tags(get_post($post_ID)->post_content) , 0, 210 , '……'); $desp = '>'.$wx_post_content.' *** [【点击链接查看全文】]('.$wx_post_link.')'; //微信推送内容正文 $key = get_option('pushbear_key'); $request = new WP_Http; $api_url = 'https://pushbear.ftqq.com/sub'; $body = array( 'sendkey' => $key, 'text' => $text, 'desp' => $desp ); $headers = 'Content-type: application/x-www-form-urlencoded'; $result = $request->post($api_url, array( 'body' => $body, 'headers' => $headers ) ); if(!is_wp_error($result)){ add_post_meta($post_ID, 'wx_pushbear_key', 1, true); } } add_action('publish_post', 'pushbear_send'); ?>
上面是代码,下面可以直接点击下载
点击下载
💡 弄了个积分可见。
如何取消
肯定有人会问,订阅之后怎么取消订阅,在订阅者和管理者那边都是可以的。
下面是 管理员界面取消
订阅者在推送信息最底端就有的
后语
有人说,这个是利用别人给自己公众号的,比较抵触这个方法,我只能说,目前来看,这个方糖公众号为公益性质的,暂时没有发送一个信息,如果对此介意但是又想使用这个功能的,可以互相导流,至于其他,那只能说随意了。
相关链接
最后贴个图
-- 完 --
-- 完 --
测试
测试下
挺好,但是我订阅号还是区别网站的内容。哈哈
@夏天烤洋芋 我也有订阅号的,但是这个和订阅号完全没关系的