代码实现WordPress主动推送及自动推送至百度搜索收录

站长们辛辛苦苦写的文章,无非就是让百度收录,也可以帮助人,也可以给自己站或者帮人优化的站带来流量,今天就来发一篇关于wordprss主动推送给百度的方法;

使用方法,U8格式放在wp当前模板functions.php里即可

/**
* 以下代码主动推送及自动推送至百度搜索收录
*/
if(!function_exists('Baidu_Submit')){
    function Baidu_Submit($post_ID) {
        $WEB_TOKEN  = 'xxxxxxxxxxxx';  //这里请换成你的网站的百度主动推送的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;
        $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',$result)) {
            add_post_meta($post_ID, 'Baidusubmit', 1, true);
        }
    }
    add_action('publish_post', 'Baidu_Submit', 0);
}

/**
* WordPress百度搜索自动推送、主动收录JS优化
* 转载请保留出处,谢谢合作!
**/
add_action( 'wp_footer', 'bdPushData', 999);
 
if(!function_exists('baidu_check_record')){
  function baidu_check_record($url,$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( BD_PUSH == 'yes' && !preg_match_all('/提交网址/u',$rs) && preg_match_all('/百度为您找到相关结果/u',$rs)){
            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;
    }
  }
}
 
if(!function_exists('bdPushData')){
  function bdPushData() {
    global $wpdb;
    $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
    $currentUrl = home_url(add_query_arg(array()));
    //这里修改了下:给get_permalink指定了文章ID
    if(baidu_check_record(get_permalink($post_id), $post_id) == 0 && $currentUrl == get_permalink($post_id)) {
        echo "<script>(function(){
            var bp = document.createElement('script');
            var curProtocol = window.location.protocol.split(':')[0];
            if (curProtocol === 'https') {
                bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
            } else {
                bp.src = 'http://push.zhanzhang.baidu.com/push.js';
            }
            var s = document.getElementsByTagName('script')[0];
            s.parentNode.insertBefore(bp, s);
            })();
            (function(){
                var src = (document.location.protocol == 'http:') ? 'http://js.passport.qihucdn.com/11.0.1.js?af9e600e6a4ba6d33cd7f1b088210cf7':'https://jspassport.ssl.qhimg.com/11.0.1.js?af9e600e6a4ba6d33cd7f1b088210cf7';
                document.write('<script src=\"' + src + '\" id=\"sozz\"><\/script>');
            })();</script>";
   }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值