Blog / WordPress/ Fixing the preg_match_all() Error in WordPress Baidu Auto-Push Code

Fixing the preg_match_all() Error in WordPress Baidu Auto-Push Code

【修正报错】WordPress百度搜索自动推送、主动收录JS代码

Problem Description

The original code published by Zhang Ge, when added to the functions.php file of some themes, may trigger a PHP warning error.

Example Error Message:

Warning: preg_match_all() expects at least 3 parameters, 2 given in ...

Cause of the Error

preg_match_all() is a PHP function for performing global regular expression matches. Its standard syntax requires at least three parameters:

preg_match_all(pattern, subject, matches)

The matches parameter stores all matching results (as an array). The original code lacked this required third parameter, causing a function call that violates syntax rules and triggers the warning.

Correction Method

Simply add the third parameter (a variable to receive the match results) to both preg_match_all() function calls in the code.

Original Code Line:

if(!preg_match_all('/提交网址/u',$rs) && preg_match_all('/百度为您找到相关结果/u',$rs)){

Corrected Code Line:

if(!preg_match_all('/提交网址/u',$rs,$mata) && preg_match_all('/百度为您找到相关结果/u',$rs,$matb)){

The variable names $mata and $matb can be customized. They are only used to receive match data and are not used later in this logic, so any valid variable name is acceptable.

Complete Corrected Code

Below is the complete corrected function code. You can copy and add it to the end of your theme's functions.php file.

/**
 * WordPress Baidu Search Auto-Push & Active Inclusion JS (Corrected Version)
 */
add_action( 'wp_footer', 'bdPushData', 999);

// Check if Baidu has indexed the page (Improved Version)
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 the keyword 【提交网址】 is NOT found in the Baidu results, the page is considered indexed.
        if(!preg_match_all('/提交网址/u',$rs,$mata) && preg_match_all('/百度为您找到相关结果/u',$rs,$matb)){
            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;
    }
  }
}

// Output Baidu Auto-Push JS Code
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"];
    }
    // Only output JS if the page is not indexed AND the current URL matches the canonical permalink.
    if(baidu_check_record(get_permalink()) == 0 && $currentUrl == get_permalink()) {
        echo "<script>(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            })();</script>";
      }
   }
}

Important Notes

  • Code Placement: Add the code to the end of your current theme's functions.php file, before the closing ?> tag (if present).
  • Function Logic: This code automatically loads Baidu's "Auto-Push" JavaScript in the site footer for pages not yet indexed by Baidu, to speed up inclusion.
  • Index Status Caching: The code caches the index status using a post meta field (baidu_record) to avoid querying Baidu on every page load, improving efficiency.
  • Server Requirements: Ensure your server's PHP environment has the curl extension enabled, otherwise the baidu_check_record function will not work.

Post a Comment

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