在functions.php中增加了如下代码:
/**
* WordPress百度搜索自动推送、主动收录JS优化
**/
add_action( 'wp_footer', 'bdPushData', 999);
//检查百度是否已收录最新改进版本
if(!function_exists('baidu_check_record')){
function baidu_check_record($url){
global $wpdb;
$post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
$baidu_record = get_post_meta($post_id,'baidu_record',true);
if( $baidu_record != 1){
$url='http://www.baidu.com/s?wd='.$url;
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$rs=curl_exec($curl);
curl_close($curl);
//如果抓取到的百度结果中不存在【提交网址】这个关键词,则认为该页面已被百度收录
if(!preg_match_all('/提交网址/u',$rs,$matches) && preg_match_all('/百度为您找到相关结果/u',$rs, $matches)){
update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true);
return 1;
} else {
return 0;
}
} else {
return 1;
}
}
}
//输出百度自动推送js代码
if(!function_exists('bdPushData')){
function bdPushData() {
if ($_SERVER['HTTPS'] != "on") {
$currentUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
} else {
$currentUrl = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
}
//判断是否是未收录页面,并且当前页面是否等于正规url地址(get_premalink)
if(baidu_check_record(get_permalink()) == 0 && $currentUrl == get_permalink()) {
echo "";
}
}
}
/**
* WordPress百度主动推送功能
**/
add_action('save_post', 'fanly_save_post_notify_baidu_zz', 10, 3);
function fanly_save_post_notify_baidu_zz($post_id, $post, $update){
if($post->post_status != 'publish') return;
$baidu_zz_api_url = 'http://data.zz.baidu.com/urls?site=your_site_url&token=dV7KPbqeUm6AO6gp';
//请到百度站长后台获取你的站点的专属提交链接
$response = wp_remote_post($baidu_zz_api_url, array(
'headers' => array('Accept-Encoding'=>'','Content-Type'=>'text/plain'),
'sslverify' => false,
'blocking' => false,
'body' => get_permalink($post_id)
));
}
评论 (0)