WordPress更新文章实时推送到百度新策略!
将下面的代码添加到你主题的functions.php中去:
/**
* WordPress发布文章主动推送到百度,加快收录保护原创【file_get_contents方式】
* 文章地址:https://www.tinggezhao.com/wp-course/666.html
*/
if(!function_exists(‘Baidu_Submit’)) {
function Baidu_Submit($post_ID) {
$WEB_TOKEN=‘xxxxxxxxx’; //这里换成你的网站的百度主动推送的token值
$WEB_DOMAIN=get_option(‘home’);
//已成功推送的文章不再推送
if(get_post_meta($post_ID,‘Baidusubmit’,true) == 1) return;
$url = get_permalink($post_ID);
$api = ‘http://data.zz.baidu.com/urls?site=’.$WEB_DOMAIN.’&token=’.$WEB_TOKEN;
$data = array (
‘http’ => array (
‘method’ => ‘POST’,
‘header’=> “Content-Type: text/plain”,
“Content-Length: “.strlen($url).”rn”,
‘content’ => $url
)
);
$data = stream_context_create($data);
$result = file_get_contents($api, false, $data);
$result = json_decode($result,true);
//如果推送成功则在文章新增自定义栏目Baidusubmit,值为1
if (array_key_exists(‘success’,$result)) {
add_post_meta($post_ID, ‘Baidusubmit’, 1, true);
}
}
add_action(‘publish_post’, ‘Baidu_Submit’, 0);
}
对,替换上面的token值为你自己的即可,这样的方法是最简单明了的
I am a student of BAK College. The recent paper competition gave me a lot of headaches, and I checked a lot of information. Finally, after reading your article, it suddenly dawned on me that I can still have such an idea. grateful. But I still have some questions, hope you can help me.