wordpress自动接入提交熊掌号非插件方法代码
WordPress 通过API提交内容到熊掌号当我们每次发布新的内容的时候,后台自动就会提交到熊掌号,免得我们每次都要自己手动提交了在主题的 function.php 中加入:
直接上代码:/**
* WordPress API 方式自动推送到百度熊掌号*
*/
if(!function_exists(‘Baidu_XZH_Submit’)){
function Baidu_XZH_Submit($post_ID) {
//已成功推送的文章不再推送
if(get_post_meta($post_ID,’BaiduXZHsubmit’,true) == 1) return;
$url = get_permalink($post_ID);
$api = ‘http://data.zz.baidu.com/urls?appid=你的APPID&token=你的TOKEN&type=realtime’;
$request = new WP_Http;
$result = $request->request( $api , array( ‘method’ => ‘POST’, ‘body’ => $url , ‘headers’ => ‘Content-Type: text/plain’) );
$result = json_decode($result[‘body’],true);
//如果推送成功则在文章新增自定义栏目BaiduXZHsubmit,值为1
if (array_key_exists(‘success’,$result)) {
add_post_meta($post_ID, ‘BaiduXZHsubmit’, 1, true);
}
}
add_action(‘publish_post’, ‘Baidu_XZH_Submit’, 0);
}
页:
[1]