Blog / WordPress/ WordPress Code for Baidu Search Inclusion: Active & Auto Push

WordPress Code for Baidu Search Inclusion: Active & Auto Push

最新版WordPress可用主动推送及自动推送至百度搜索收录代码

WordPress Baidu Search Inclusion Code (Active Push & Auto Push)

The following code integrates two inclusion methods provided by Baidu Search Resources Platform: Active Push (Real-time) and Auto Push (JS Push). The code is sourced from the web and has been optimized and corrected.

Feature Description

  • Active Push: Immediately pushes the article link to Baidu via API upon publication for the fastest inclusion speed.
  • Auto Push: Automatically pushes the current page URL to Baidu via embedded JavaScript code when a visitor views the page.
  • Inclusion Status Check: Checks if the article is already indexed by Baidu before auto-pushing to avoid duplicate submissions.

Usage Instructions

Add the complete code below to the end of your theme's functions.php file, or add it via a "Code Snippets" plugin.

Important Note: Before use, replace xxxxxxxxxxxx in the code with your own "Active Push" interface Token obtained from the Baidu Search Resources Platform.

/**
 * WordPress Baidu Search Inclusion Optimization Code (Active Push + Auto Push)
 * Active push code modified from Zhang Ge's blog, auto push includes inclusion check logic
 */

// 1. Active Push Function (Triggered on post publish)
if(!function_exists('Baidu_Submit')){
    function Baidu_Submit($post_ID) {
        // Replace with your Baidu Active Push Token
        $WEB_TOKEN  = 'xxxxxxxxxxxx';
        $WEB_DOMAIN = get_option('home');

        // Skip if already successfully pushed
        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'
        ));

        if (is_wp_error($result)) {
            // Log failure here if needed
            return;
        }

        $result = json_decode($result['body'], true);

        // Mark post if push succeeded
        if (is_array($result) && array_key_exists('success', $result)) {
            add_post_meta($post_ID, 'Baidusubmit', 1, true);
        }
    }
    add_action('publish_post', 'Baidu_Submit', 0);
}

// 2. Helper: Check if URL is indexed by Baidu
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) return 1;

        $search_url = 'https://www.baidu.com/s?wd=site:' . urlencode($url);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $search_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)');
        $rs = curl_exec($curl);
        curl_close($curl);

        if (!strpos($rs, '很抱歉,没有找到与') && preg_match('/' . preg_quote($url, '/') . '/i', $rs)) {
            update_post_meta($post_id, 'baidu_record', 1) || add_post_meta($post_id, 'baidu_record', 1, true);
            return 1;
        } else {
            return 0;
        }
    }
}

// 3. Auto Push Function (Insert JS in page footer)
if(!function_exists('bdPushData')){
    function bdPushData() {
        if(!is_singular()) return;
        $post_id = get_the_ID();
        if(!$post_id) return;
        $current_url = get_permalink($post_id);

        if(baidu_check_record($current_url, $post_id) == 0) {
            $push_js = "<script>n(function(){n    var bp = document.createElement('script');n    var curProtocol = window.location.protocol.split(':')[0];n    if (curProtocol === 'https') {n        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';n    } else {n        bp.src = 'http://push.zhanzhang.baidu.com/push.js';n    }n    var s = document.getElementsByTagName('script')[0];n    s.parentNode.insertBefore(bp, s);n})();n</script>";
            echo $push_js;
        }
    }
    add_action('wp_footer', 'bdPushData', 999);
}

Code Optimizations

Compared to the original, this rewrite includes:

  1. Error Handling: Added basic error checking (is_wp_error) for the active push API request.
  2. Improved Index Check: Uses a more precise site:url query and mimics the Baidu spider User-Agent for better accuracy.
  3. Execution Limits: Auto-push JS only loads on singular posts/pages (is_singular()), not on home or archive pages.
  4. Removed Redundant Code: Deleted an unrelated second JS snippet referencing "Qihu" from the original auto-push code.
  5. Clearer Structure: Added clearer comments and separated the three functional modules.

Important Notes

  • Baidu now recommends the HTTPS version of the push JS (https://zz.bdstatic.com/linksubmit/push.js); the code is adapted.
  • The inclusion check function (baidu_check_record) relies on remote requests to Baidu's search results. Frequent use may slow page loads or trigger anti-crawling measures. Consider running it as a scheduled task in production.
  • Successful pushes or confirmed inclusion adds Baidusubmit and baidu_record custom fields as markers, visible in the post editor's "Custom Fields" area.
  • Ensure your server supports and allows external HTTP requests (WP_Http and curl_init) for the code to function.

Post a Comment

Your email will not be published. Required fields are marked with *.