- A+
将下列代码放入functions.php中,(该代码是根据WordPress主动推送到百度代码版改造而来,原使用过主动推送代码的将其替换.)
/**
* WordPress发布文章主动推送到百度熊掌号
*/
if(!function_exists('Baidu_Submit')){
function Baidu_Submit($post_ID) {
//已成功推送的文章不再推送
if(get_post_meta($post_ID,'Baidusubmit',true) == 1) return;
$url = get_permalink($post_ID);
$copy = get_post_meta($post->ID, 'wzurl', true);
if (empty($copy)) {
$api = 'http://data.zz.baidu.com/urls?appid=你的熊掌号ID&token=你的准入密钥&type=realtime,original';
}else{
$api = 'http://data.zz.baidu.com/urls?appid=你的熊掌号ID&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);
//如果推送成功则在文章新增自定义栏目Baidusubmit,值为1
if (array_key_exists('success_realtime',$result)) {
add_post_meta($post_ID, 'Baidusubmit', 1, true);
}
}
add_action('publish_post', 'Baidu_Submit', 0);
}
其中该代码默认将发表的文章默认为原创推送到百度,如果是非原创文章请将文章的自定义字段wzurl的值添加为文章的链接地址即可.